二叉树最大深度和最小深度
2017-05-22 21:35
399 查看
二叉树的最大深度
int maxDepth(TreeNode *root) { // write your code (here) if(root == NULL) return 0; int leftDepth = maxDepth(root->left); int rightDepth = maxDepth(root->right); return leftDepth > rightDepth ? (leftDepth + 1) : (rightDepth + 1); }
二叉树的最小深度
int minDepth(TreeNode *root) { // write your code here if(root == NULL) return false; if(root->left == NULL && root->right == NULL) return 1; int leftDepth = minDepth(root->left); if(leftDepth == 0) leftDepth = INT_MAX; int rightDepth = minDepth(root->right); if(rightDepth == 0) rightDepth = INT_MAX; return leftDepth < rightDepth ? (leftDepth + 1) : (rightDepth + 1); }
相关文章推荐
- Leetcode 95. Unique Binary Search Trees II及二叉树最大最小深度镜像树总结
- python_lintcode_97二叉树的最大深度 _155二叉树的最小深度
- 二叉树的最大深度和最小深度实现
- 二叉树最大深度最小深度之微妙区别
- Maximum and Minimum Depth of Binary Tree 二叉树的最小最大深度 [java]
- 二叉树的最小高度,最大高度(深度)和宽度
- 每天一道LeetCode-----计算二叉树的最大深度及最小深度,判断二叉树是否是高度平衡二叉树
- 二叉树的最大深度与最小深度
- 二叉树最大 最小深度
- 二叉树的最大深度 & 二叉树的最小深度
- LeetCode—二叉树的最大最小深度
- 求二叉树的最小深度和最大深度
- 二叉树的最大深度与最小深度
- leetcode之二叉树类之二叉树深度系列-----104/111/110/108/109 二叉树最大/最小深度/AVL树的判断和由有序序列生成(牵扯分治相关,OJ105/106)
- 二叉树最大深度和最小深度
- 二叉树的最大深度 & 二叉树的最小深度
- 面试题16:递归计算二叉树的最大、最小深度
- 二叉树的最大深度与最小深度
- [Java代码] [Leetcode] Maximum and Minimum Depth of Binary Tree 二叉树的最小最大深度
- 求二叉树的最大深度和最小深度以及之间的差