关于二叉树的一些心得

本文探讨了二叉树的各种遍历方法,包括递归与非递归的先序、中序、后序遍历,以及广度优先搜索。同时,介绍了如何判断二叉树是否为完全二叉树、满二叉树、二叉排序树以及平衡二叉树,并提供了相应的实现代码。
摘要由CSDN通过智能技术生成

#include
#include
#include
#include

using namespace std;

typedef struct bitnode
{

int val;
struct bitnode* lchild, * rchild;

}bitnode,*bitree;

struct ReturnTypeOne
{

bool isBST;
int _min;
int _max;

};

struct ReturnTypeTwo
{

bool isAVL;
int height;
ReturnTypeTwo(bool _isAVL, int _height)
{
    isAVL = _isAVL;
    height = _height;
}

};

int StringToInt(string s)
{

int num = 0;
for (int i = 0; i < s.size(); i++)
{
    num = num * 10 + s[i] - '0';
}
return num;

}

int preValue = INT_MIN;

//构建一颗二叉树
bitree createBitree(int level)
{

string s;
cout << "this is the " << level << "th's bitnode:" << endl;
cin >> s;
if (s == "n") {
	return nullptr;
}
else {
	int x = StringToInt(s);
	
	bitree node = new bitnode;

	node->val = x;
	node->lchild = createBitree(level + 1);
	node->rchild = createBitree(level + 1);
	return node;
}

}

//递归的先序遍历
void preOrder(bitree node)
{
if (!node) return;

cout << node->val << ' ';
preOrder(node->lchild);
preOrder(node->rchild);

}

//非递归的先序遍历
void PreOrder(bitree node)
{


                
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

虐鼠无情麻子刘

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值