lintcode minimum-depth-of-binary-tree 二叉树最小深度
来源:互联网 发布:研华数据采集卡 编辑:程序博客网 时间:2024/05/19 22:56
问题描述
lintcode
笔记
与求最大深度不同的是,求最小深度要考虑到左孩子或者右孩子为空的情况。如果左孩子或者右孩子为空,空的那一边不能用来计算深度,因此要求max(取非空那一边的深度),而不是求min。
代码
/** * Definition of TreeNode: * class TreeNode { * public: * int val; * TreeNode *left, *right; * TreeNode(int val) { * this->val = val; * this->left = this->right = NULL; * } * } */class Solution {public: /** * @param root: The root of binary tree. * @return: An integer */ int minDepth(TreeNode *root) { // write your code here if (root == NULL) return 0; int ldepth = minDepth(root->left); int rdepth = minDepth(root->right); if (root->left == NULL || root->right == NULL) { return max(ldepth, rdepth) + 1; } else { return min(ldepth, rdepth) + 1; } }};
0 0
- lintcode minimum-depth-of-binary-tree 二叉树最小深度
- LeetCode Minimum Depth of Binary Tree 最小深度二叉树
- Minimum Depth of Binary Tree 二叉树的最小深度
- Minimum Depth of Binary Tree 二叉树最小深度
- Minimum Depth of Binary Tree 二叉树的最小深度
- Leetcode Minimum Depth of Binary Tree 二叉树最小深度
- Minimum Depth of Binary Tree 二叉树的最小深度
- LeetCode:minimum-depth-of-binary-tree(二叉树最小深度)
- Minimum Depth of Binary Tree 求二叉树最小高度/深度@LeetCode
- 求二叉树的最小深度 Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree -- 二叉树的最小深度 DFS 加剪枝
- LeetCode OJ 之 Minimum Depth of Binary Tree(二叉树的最小深度)
- leetCode 111.Minimum Depth of Binary Tree(二叉树最小深度) 解题思路和方法
- [LeetCode-111] Minimum Depth of Binary Tree (二叉树最小深度)
- [Java代码] [Leetcode] Maximum and Minimum Depth of Binary Tree 二叉树的最小最大深度
- 111. Minimum Depth of Binary Tree 二叉树的最小深度
- LeetCode 111. Minimum Depth of Binary Tree(二叉树最小深度)
- 111. Minimum Depth of Binary Tree 二叉树的最小深度
- 安卓端发送短信
- 4-4 求自定类型元素的平均 (10分)
- IntelliJ IDEA中如何设置30分钟自动刷新svn
- 陈伟霆,赵丽颖,李易峰,青云志,分析。
- 美素数<hdoj4548>
- lintcode minimum-depth-of-binary-tree 二叉树最小深度
- MYSQL的wait_timeout与连接池相关
- listView分割线的相关设置
- caffe源码学习:layer
- docker4dotnet #2 容器化主机
- Codelgniter框架使用UEditor问题。
- Linux内核移植与启动
- android补间动画(Tween Animation)
- ORA-00600: 内部错误代码, 参数: [kcratr_scan_lastbwr], [], [], [], [], [], [], [], [], [], [], []