Tree-----求树的最大深度和最小深度(104. Maximum Depth of Binary Tree 111. Minimum Depth of Binary Tree)
来源:互联网 发布:河北农业科技网络书屋 编辑:程序博客网 时间:2024/05/19 21:17
1.最大深度
原题目
public class Solution { public int maxDepth(TreeNode root) { if (root == null) { return 0; } int a = 0, b = 0; if (root.left != null) { a = maxDepth(root.left) + 1; } if (root.right != null) { b = maxDepth(root.right) + 1; } //只有一个节点的特殊情况 if (a == 0 && b == 0) { return 1; } if (a > b) { return a; } else { return b; } }}
2.最小深度
public int minDepth(TreeNode root) { if (root == null) { return 0; } if (root.left == null) { return minDepth(root.right) + 1; } if (root.right == null) { return minDepth(root.left) + 1; } return Math.min(minDepth(root.right), minDepth(root.left)) + 1; }
0 0
- Tree-----求树的最大深度和最小深度(104. Maximum Depth of Binary Tree 111. Minimum Depth of Binary Tree)
- [Java代码] [Leetcode] Maximum and Minimum Depth of Binary Tree 二叉树的最小最大深度
- Maximum and Minimum Depth of Binary Tree 二叉树的最小最大深度 [java]
- Leetcode:111. Minimum Depth of Binary Tree(求Tree的最小的深度)
- leetcode Minimum&&Max Depth of Binary Tree 求树的最小&&最大深度
- Minimum Depth of Binary Tree 二叉树的最小深度
- Minimum Depth of Binary Tree 二叉树的最小深度
- Minimum Depth of Binary Tree 二叉树的最小深度
- Maximum Depth of Binary Tree 二叉树的最大深度
- Maximum Depth of Binary Tree 二叉树的最大深度
- Maximum Depth of Binary Tree 二叉树的最大深度
- Maximum Depth of Binary Tree 二叉树的最大深度
- maximum-depth-of-binary-tree(二叉树的最大深度)
- 【二叉树】计算二叉树深度【104. Maximum Depth of Binary Tree】【111. Minimum Depth of Binary Tree】
- leetcode解题之Maximum/Minimum Depth of Binary Tree Java版(树的最大、最小深度)
- Minimum Depth of Binary Tree(求二叉树的最小深度)
- 104. Maximum Depth of Binary Tree 求树的最大深度
- [leetcode]Maximum Depth of Binary Tree (求二叉树的最大深度 C语言)
- MySQL2-字段类型和字段属性
- 不能拖动CAD文件到CAD窗口打开,解决方案
- Linux下MySQL导入文件出错ERROR 1290 (HY000)
- JavaScript语言基础---(八)对象
- 漫谈程序员系列:快来约这些女生,保你脱单
- Tree-----求树的最大深度和最小深度(104. Maximum Depth of Binary Tree 111. Minimum Depth of Binary Tree)
- JS排他思想(实现导航栏)
- 胡适致毕业生:在不健全的中国,如何不堕落
- 漫谈程序员系列:神奇的四步编程法
- 几款主流 NoSql 数据库的对比
- MySql修改端口号,修改my.ini的端口号就可以了,一般情况下都在安装目录下
- Linux服务器安全登录注意事项
- PHP foreach的两种用法
- Struts Spring com/opensymphony/xwork2/spring/SpringObjectFactory.java:220:-1 的一点解决办法