二元树的深度 【微软面试100题 第五十二题】
题目要求:
输入一颗二叉树的根结点,求该树的深度。
从根结点到叶结点依次经过的结点(含根、叶结点)形成树的一条路径,最长路径的长度为树的深度。
如 3
/ \
4
\
2,深度为3
参考链接:剑指offer第39题。
题目分析:
用递归的方式从根结点开始,遍历其左右结点,较大值则为该树的深度。
代码实现:
#include <iostream> #include <stack> using namespace std; typedef struct BinaryTree { struct BinaryTree *left,*right; int data; }BinaryTree; void initTree(BinaryTree **p); int FindTreeDepth(BinaryTree *root); int main(void) { BinaryTree *root; initTree(&root); cout << "二叉树深度为:" << FindTreeDepth(root) << endl; return 0; } int FindTreeDepth(BinaryTree *root) { if(root==NULL) return 0; int left = FindTreeDepth(root->left); int right = FindTreeDepth(root->right); return (left>right) ? (left+1):(right+1); } // 10 // / \ // 5 12 // / \ // 4 7 void initTree(BinaryTree **p) { *p = new BinaryTree; (*p)->data = 10; BinaryTree *tmpNode = new BinaryTree; tmpNode->data = 5; (*p)->left = tmpNode; tmpNode = new BinaryTree; tmpNode->data = 12; (*p)->right = tmpNode; tmpNode->left = NULL; tmpNode->right = NULL; BinaryTree *currentNode = (*p)->left; tmpNode = new BinaryTree; tmpNode->data = 4; currentNode->left = tmpNode; tmpNode->left = NULL; tmpNode->right = NULL; tmpNode = new BinaryTree; tmpNode->data = 7; currentNode->right = tmpNode; tmpNode->left = NULL; tmpNode->right = NULL; cout << "二叉树为:" <<endl; cout << " " << 10<<endl; cout << " " <<"/" << " "<< "\\" <<endl; cout << " " << 5 << " " << 12 << endl; cout << " " <<"/" << " "<< "\\" <<endl; cout << 4 << " " << 7 << endl; }
题目扩展:
输入一颗二叉树的根结点,判断该树是不是平衡二叉树。如果某二叉树中任意结点的左右子树的深度相差不超过1,那么它就是一颗平衡二叉树。
如 3
/ \
4
\
2,不是平衡二叉树,因为3的左子树深度为2,右子树深度为0,差超过1
如 3
/ \
4 5
\
2 ,是平衡二叉树,因为3的左子树深度为2,右子树深度为1,差不超过1;同理4、5、2的左右子树深度都不超过1,则是平衡。
参考剑指offer第39题。
题目分析:
用后序遍历的方式遍历二叉树的每个结点(左+右+中),在遍历到一个结点前我们就已经遍历了它的左右子树。只要在遍历每个结点的时候记录它的深度,我们就可以一边遍历一边判断每个结点是不是平衡的。
代码实现:
#include <iostream> #include <stack> using namespace std; typedef struct BinaryTree { struct BinaryTree *left,*right; int data; }BinaryTree; void initTree(BinaryTree **p); bool IsBalanced(BinaryTree *root); int main(void) { BinaryTree *root; initTree(&root); int isBalanced = IsBalanced(root); if(isBalanced) cout << "是平衡树" << endl; else cout << "不是平衡树" << endl; return 0; } bool IsBalanced(BinaryTree *root,int *depth) { if(root==NULL) { *depth = 0; return true; } int left,right; if(IsBalanced(root->left,&left) && IsBalanced(root->right,&right)) { int dif = left-right; if(dif<=1 && dif>=-1) { *depth = 1+(left>right?left:right); return true; } } return false; } bool IsBalanced(BinaryTree *root) { int depth = 0; return IsBalanced(root,&depth); } // 10 // / \ // 5 12 // / \ // 4 7 void initTree(BinaryTree **p) { *p = new BinaryTree; (*p)->data = 10; (*p)->right = NULL; BinaryTree *tmpNode = new BinaryTree; tmpNode->data = 5; (*p)->left = tmpNode; //加上这里就是平衡树,不加就是上图中去掉了12 /* tmpNode = new BinaryTree; tmpNode->data = 12; (*p)->right = tmpNode; tmpNode->left = NULL; tmpNode->right = NULL; */ BinaryTree *currentNode = (*p)->left; tmpNode = new BinaryTree; tmpNode->data = 4; currentNode->left = tmpNode; tmpNode->left = NULL; tmpNode->right = NULL; tmpNode = new BinaryTree; tmpNode->data = 7; currentNode->right = tmpNode; tmpNode->left = NULL; tmpNode->right = NULL; cout << "二叉树为:" <<endl; cout << " " << 10<<endl; cout << " " <<"/" << " "<< "\\" <<endl; cout << " " << 5 << " " << 12 << endl; cout << " " <<"/" << " "<< "\\" <<endl; cout << 4 << " " << 7 << endl; }
很多时候不是我们做不好,而是没有竭尽全力......
分类:
【微软面试100题】
posted on 2014-11-11 14:07 tractorman 阅读(183) 评论(0) 编辑 收藏 举报
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?