二叉树进阶问题

上回说了有关于二叉树的一些基本问题,例如前序遍历。不知道UU们有没有忘了啊,有时间可以回过头去复习一下呢,这些都是一些基础常见问题。
闲话少叙,今天给大家介绍一些二叉树进阶问题。Begin!

阅读前的说明,本文部分代码演示部分函数及
自定义变量实现过程未写出;在此统一说明
struct bintree{
	int val;
	struct bintree* left;
	struct bintree* right;
}bintree;
queueEmpty() //队列判空
queuePush() //入队
queuePop() //出队
queuefront()//取队头元素
queueInit()queueDestroy() //初始化、销毁

层序遍历:

顾名思义,就是按照树的每一层每一层遍历节点,例如在这里插入图片描述
的结果应该是1 2 3 4 5 6 7
由于每一次打印数据都会出现左右子树同时访问的情况,因此针对层序遍历通过递归实现难度比较大,因此我们借助队列结构先入先出的特性来辅助我们完成目的。
分析: 在未遇到空节点的情况下,我们从根结点开始访问,将根节点入队来保证一开始队列不为空,后续只需将根节点出队的同时使根节点的左右节点依次入队,直至遍历到叶子节点。

void LevelOrder(bintree* root)
{
	 queue q;//声明一个队列结构
	 queueInit(&q);
	 if(root)
	 	queuePush(&q,root);
	 while(!queueEmpty(&q)){
	 	bintree* front=queuefront(&q);
	 	queuePop(&q);
	 	printf("%d ",front->val);
	 	if(front->left)
	 		queuePush(&q,front->left);
	 	if(front->right)
	 		queuePush(&q,front->right);
	 }
	 queueDestroy(&q);
}

查找节点(返回地址):请添加图片描述

bintree* TreeFind(bintree* root, int x)
{
	if (root == NULL)
		return NULL;
		
	if (root->data == x)
		return root;

	bintree* ret1 = TreeFind(root->left, x);
	if (ret1)
		return ret1;

	bintree* ret2 = TreeFind(root->right, x);
	if (ret2)
		return ret2;
	return NULL;
}

二叉树高度:

分析: 将每一个节点的左右子树高度求出取大+1即树的高度

int treeheight(bintree* root)
{
	if(root==NULL)
		return 0;
	int left=treeheight(root->left);
	int right=treeheight(root->right);//必须记录,否则可能会造成数据丢失
	return left>right?left+1:right+1;
}

二叉树相同判断

https://leetcode-cn.com/problems/same-tree/
分析:
结束条件:
1、同时遇到空节点
2、一空一不空
3、值不相等

bool issametree(bintree* p,bintree* q)
{
	if(p==NULL&&q==NULL)
		return true;
	if(p==NULL||q==NULL)
		return false;
	if(p->val!=q->val)
		return false;
	return issametree(p->left,q->left)&&issametree(p->right,q->right);

子树问题:

https://leetcode-cn.com/problems/subtree-of-another-tree/
给定两棵二叉树,判断一个树是否为另一棵树的子树
只需要判断其本身是否与另一棵树中的子树相同

bool sontree(bintree* p,bintree* q)
{
	if(p==NULL)
		return false;
	if(issametree(p,q))
		return true;
	return sontree(p->left,q)||sontree(p->right,q);
}

单值二叉树:

检查树的元素是否都相同,根据等式的传递性可知,只需要判断每个根和其左右子树的数量关系

bool isUnivalTree(bintree* root)
{
	if(root==NULL)
		return true;
	 if(root->left&&root->left->val!=root->val)
        return false;
    if(root->right&&root->right->val!=root->val)
        return false;
    return isUnivalTree(root->right)&&isUnivalTree(root->left);
}

平衡二叉树:

https://leetcode-cn.com/problems/balanced-binary-tree/

int treeheight(struct TreeNode* root)
{
    if(root==NULL)
        return 0;
    int ret1=treeheight(root->left);
    int ret2=treeheight(root->right);
    return ret1>ret2?ret1+1:ret2+1;
}
bool isBalanced(struct TreeNode* root) {
    if(root==NULL)
        return true;
    if(abs(treeheight(root->left)-treeheight(root->right))>1)
        return false;
    return isBalanced(root->left)&&isBalanced(root->right);
}

对称二叉树:

https://leetcode-cn.com/problems/symmetric-tree/

bool _isSymmetric(struct TreeNode* leftroot,struct TreeNode* rightroot)
{   
    if(leftroot==NULL&&rightroot==NULL)
        return true;
    if(leftroot==NULL||rightroot==NULL)
        return false;
    if(leftroot->val!=rightroot->val)
        return false;
    return _isSymmetric(leftroot->left,rightroot->right)&&_isSymmetric(leftroot->right,rightroot->left);
    
}
bool isSymmetric(struct TreeNode* root) {
    return _isSymmetric(root->left,root->right);
}

翻转二叉树:

https://leetcode-cn.com/problems/invert-binary-tree/

struct TreeNode* invertTree(struct TreeNode* root) {
    if(root==NULL)
        return NULL;
    struct TreeNode* left=invertTree(root->left);
    struct TreeNode* right=invertTree(root->right);
    root->left=right;
    root->right=left;
    return root;
}

喜欢的UU可以给个关注给个赞迈,蟹蟹!

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Shall#

你的鼓励将是我前进的动力

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

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

打赏作者

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

抵扣说明:

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

余额充值