LeetCode111. Minimum Depth of Binary Tree
来源:互联网 发布:目标 星辰大海 知乎 编辑:程序博客网 时间:2024/05/22 04:25
LeetCode111.Minimum Depth of Binary Tree
Given a binary tree, find its minimum depth.
The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.
由题意,要寻找该二叉树的最小深度值,即要找到最近的那个叶子节点。假若用DFS,要不停搜寻、排错,而用BFS,一圈圈那样扩展下去,找到第一个子节点即可停止,因此要方便适合得多。
代码如下:
/** * 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 minDepth(TreeNode* root) { if (root == NULL) return 0; int left_minDepth = minDepth(root->left); int right_minDepth = minDepth(root->right); if (left_minDepth == 0) return right_minDepth + 1; if (right_minDepth == 0) return left_minDepth + 1; return left_minDepth > right_minDepth?right_minDepth + 1:left_minDepth + 1; }};
阅读全文
0 0
- leetcode111---Minimum Depth of Binary Tree
- LeetCode111—Minimum Depth of Binary Tree
- LeetCode111:Minimum Depth of Binary Tree
- [leetcode111].Minimum Depth of Binary Tree
- leetcode111~Minimum Depth of Binary Tree
- LeetCode111 Minimum Depth of Binary Tree
- leetcode111. Minimum Depth of Binary Tree
- LeetCode111. Minimum Depth of Binary Tree
- LeetCode111. Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Problem M (树状数组)
- android:碎片的使用方式
- 原码,反码,补码和移码
- 事务的传播行为
- HBase数据导出到HDFS
- LeetCode111. Minimum Depth of Binary Tree
- 周末总结--2017.10.8
- u-boot 2013.10中DM9000的移植经
- Java traps之日期格式化
- Adobe Air ANE之Admob开发需要的依赖库
- Tomcat
- Median of Two Sorted Arrays
- 软件测试——测试术语
- LocalStorage和sessionStorage之间的区别