上回说了有关于二叉树的一些基本问题,例如前序遍历。不知道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可以给个关注给个赞迈,蟹蟹!