LEETCODE: Minimum Depth of Binary Tree
来源:互联网 发布:zabbix源码包下载 编辑:程序博客网 时间:2024/06/05 06:49
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.
/** * Definition for binary tree * 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; if (root->left == NULL && root->right == NULL) return 1; int leftDepth = minDepth(root->left); int rightDepth = minDepth(root->right); if (leftDepth == 0) return rightDepth + 1; else if (rightDepth == 0) return leftDepth + 1; else return min(leftDepth, rightDepth) + 1; }};
0 0
- LeetCode: Minimum Depth of Binary Tree
- [Leetcode] Minimum Depth of Binary Tree
- LeetCode : Minimum Depth of Binary Tree
- leetcode 31: Minimum Depth of Binary Tree
- [LeetCode] Minimum Depth of Binary Tree - BFS
- [LeetCode] Minimum Depth of Binary Tree - DFS
- 【leetcode】Minimum Depth of Binary Tree
- LeetCode:Minimum Depth of Binary Tree
- [LeetCode]Minimum Depth of Binary Tree
- [Leetcode]Minimum Depth of Binary Tree
- [leetcode]Minimum Depth of Binary Tree
- Leetcode: Minimum Depth of Binary Tree
- LeetCode-Minimum Depth of Binary Tree
- [leetcode] Minimum Depth of Binary Tree
- LeetCode - Minimum Depth of Binary Tree
- 【Leetcode】Minimum Depth of Binary Tree
- LeetCode | Minimum Depth of Binary Tree
- LeetCode - Minimum Depth of Binary Tree
- 医疗时鲜资讯:杂谈“家庭医生”、“远程医疗”、“移动医疗”、“健康大数据”
- 分布式存储和计算总结
- C++ 友元、组合、继承的简单使用
- Linux中两种包:源代码包、二进制包(也叫RPM包、系统默认包)以及他们各自的优缺点
- 用Eclipse JEE做JSP开发时,需要对Eclipse JEE部署环境变量
- LEETCODE: Minimum Depth of Binary Tree
- 顺序容器(二)--《C++ primer》
- 红黑树算法解析
- 未在本地计算机上注册“Microsoft.Jet.OLEDB.4.0”的解决方法
- autoMapper(二)--应用场景
- Android程序的基本组件
- 用JS判断两个数字的大小
- 2014年终总结
- 总结过去,迎接2015