LeetCode——Maxium Depth of Binary Tree
LeetCode——Maxium Depth of Binary Tree
# 104
Given a binary tree, find its maximum depth.The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
这一题的目的是求出二叉树中的最大深度。显然用递归的方式是可以实现这一题的。无论是DFS还是BFS都是可以的。
- C++
/*** Definition for a binary tree node.* struct TreeNode {* int val;* TreeNode *left;* TreeNode *right;* TreeNode(int x) : val(x), left(NULL), right(NULL) {}* };*/
class Solution {int max_depth = 0;
public:int maxDepth(TreeNode* root) {if(!root)return 0;return max_Depth(root,1);}int max_Depth(TreeNode* root,int now_depth) {if(!root)return 0;if(now_depth > max_depth) {max_depth = now_depth;}max_Depth(root -> left,now_depth + 1);max_Depth(root -> right,now_depth + 1);return max_depth; }
};
还有一种方法,也是DFS,更加简洁。
- C++
/*** Definition for a binary tree node.* struct TreeNode {* int val;* TreeNode *left;* TreeNode *right;* TreeNode(int x) : val(x), left(NULL), right(NULL) {}* };*/
class Solution {
public: int maxDepth(TreeNode *root) { if(root == NULL) return 0; return 1 + max( maxDepth(root->left), maxDepth(root->right) ); }
};
除了DFS,BFS也能实现这个题目,但相对而言,BFS稍微复杂一点,需要借助到其他的结构,队列或者栈。
- C++/queue
/*** Definition for a binary tree node.* struct TreeNode {* int val;* TreeNode *left;* TreeNode *right;* TreeNode(int x) : val(x), left(NULL), right(NULL) {}* };*/
class Solution {
public: int maxDepth(TreeNode *root) { if(root == NULL) return 0; queue<TreeNode *> Q; Q.push(root); int count = 1; int depth = 0; while(!Q.empty()){ TreeNode *tmp = Q.front(); Q.pop(); count--; if(tmp->left){ Q.push(tmp->left); } if(tmp->right){ Q.push(tmp->right); } if(count == 0){ depth++; count = Q.size();} } return depth; }
};
- C++/stack
class Solution { public: int maxDepth(TreeNode *root) { // Start typing your C/C++ solution below // DO NOT write int main() function if(root == NULL) return 0; stack<TreeNode*> S; int maxDepth = 0; TreeNode *prev = NULL; S.push(root); while (!S.empty()) { TreeNode *curr = S.top(); if (prev == NULL || prev->left == curr || prev->right == curr) { if (curr->left) S.push(curr->left); else if (curr->right) S.push(curr->right); } else if (curr->left == prev) { if (curr->right) S.push(curr->right); } else { S.pop(); } prev = curr; if (S.size() > maxDepth) maxDepth = S.size(); } return maxDepth; } };
LeetCode——Maxium Depth of Binary Tree相关推荐
- LeetCode:Minimum Depth of Binary Tree,Maximum Depth of Binary Tree
LeetCode:Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth ...
- LeetCode——Maximum Depth of Binary Tree
LeetCode--Maximum Depth of Binary Tree Question Given a binary tree, find its maximum depth. The max ...
- leetcode - Minimum Depth of Binary Tree
题目:Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is th ...
- Leetcode: Maximum Depth of Binary Tree
题目:算出二叉树的最大深度 解决方案:(1)BFS (2)DFS (1)BFS 一层一层往下搜索,一直找到最深的点,这里由于节点的val是没有用的,所以可以用来存储当前节点的深度,然后注意指针一定要初 ...
- LeetCode Maximum Depth of Binary Tree
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...
- LeetCode Minimum Depth of Binary Tree
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...
- [LeetCode] Minimum Depth of Binary Tree
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...
- LeetCode - Maximum Depth of Binary Tree
递归求二叉树的最大深度. /*** Definition for binary tree* public class TreeNode {* int val;* TreeNode left;* Tre ...
- LeetCode | Minimum Depth of Binary Tree
题目:给定一个二叉树,找到其最小深度.最小深度是从根节点到最近叶节点的最短路径的节点数. 1 /** 2 * Definition for binary tree 3 * public class T ...
- leetcode:Minimum Depth of Binary Tree【Python版】
1.类中递归调用添加self: 2.root为None,返回0 3.root不为None,root左右孩子为None,返回1 4.返回l和r最小深度,l和r初始为极大值: 1 # Definition ...
最新文章
- 《编程导论(Java)#183;1.4.1 范式》
- HIDL示例-C++服务创建Client验证-Android10.0 HwBinder通信原理(三)
- http请求的3位返回码简单解释
- 程序猿修仙之路--算法之直接插入排序
- linux下的/dev/shm/ 以及与swap目录的区别【转】
- html嵌入war_WAR文件与具有嵌入式服务器的Java应用程序
- 类似flashget的浮动窗口的实现
- 如何在开源社区贡献代码_在社区支持大量涌现之后,Biicode便开始开源
- 现在更新鸿蒙会成为小白鼠吗,如果荣耀Magic3搭载了屏下镜头和鸿蒙系统,你会做第一批吗?...
- Spring beans配置方案(一) 学习笔记
- C++ ---------- map的使用
- 13. PHP 数组
- Windows 7 修改系统临时文件夹
- 网络连接 断断续续 出现黄色感叹号! 网速缓慢
- Python--发送邮件和钉钉消息
- windows11配置检测工具-win11配置检测工具
- 自定义右键的打开方式
- go语言下载gin失败解决方案
- sstream和strstream的区别
- 正则表达式匹配居民身份证
热门文章
- 苹果cms vod.html,苹果cms
- 评:10月PMI指数新高, 带动大盘逆转, 跨年度业绩行情展开
- Hungry for your love 真爱无限
- Centos7之Hadoop完全分布式集群搭建和配置
- 外企常用英语词汇或短语
- 蝙蝠侠大战超人:正义黎明[Batman v Superman: Dawn of Justice]
- Emulator: Process finished with exit code -1073741515 (0xC0000135)错误
- 【转】【干案例】江小白的微博营销 看完直接给跪了!
- 2022年鲜花行业发展趋势
- 答题微信小程序实现(5):大功告成,整个模板,题库导入/切换/积分/选择对错判断/闯关成功