高度 | 交换 | 打印二叉树 | 二叉树树形可视化 | C

BinaryTreeNode.cpp

/*设二叉树以二叉链表方式存储,试编写求解下列问题的递归算法。
设二叉树结点和二叉树结构体定义如下:
(1)求一棵二叉树的高度;
(2)求一棵二叉树中的结点个数;
(3)交换一棵二叉树中每个结点的左、右子树。*/

#include<iostream>
#include<cstring>
#include <stdio.h>
#include<stdlib.h>
#include "Queue.h"
#include <math.h>
#include "BinaryTreeNode.h"

using namespace std;


BinaryTreeNode* PreCreateBt(BinaryTreeNode *&t) //先序创建二叉树
{
   
   

    char ch;
	ch = getchar();
	fflush(stdin);
    if (ch == '#'){
   
   
        t = NULL;
    }
    else
    {
   
   
        t = (BinaryTreeNode *)malloc(sizeof(BinaryTreeNode));
        t->Data = ch;
        printf("left Child:\n");
        t->lChild = PreCreateBt(t->lChild);
        printf("right Child:\n");
        t->rChild = PreCreateBt(t->rChild);
    }
	return t;
}

int Height(BinaryTreeNode *t){
   
   
    if(t == NULL){
   
   
        return 0;
    }
    if(t->lChild == NULL && t->rChild == NULL){
   
   
        return 1;
    }
    return max(Height(t->lChild),Height(t->rChild)) + 1;
}

void Swap(BinaryTreeNode *t){
   
   
    if(t == NULL)
        return;
    else{
   
   
        BinaryTreeNode *temp = t->lChild;
        t->lChild = t->rChild;
        t-
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值