第十八周:[Leetcode]111. Minimum Depth of Binary Tree
来源:互联网 发布:淘宝排除王 编辑:程序博客网 时间:2024/06/05 14:57
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.
和求最大距离不一样,因为是求到叶子节点的最小距离,需要区分左右子树有可能为空的情况。
class Solution {public: int minDepth(TreeNode* root) { if(root == NULL) return 0; if(root -> left == NULL && root -> right == NULL) return 1; if(root -> left != NULL && root -> right == NULL) return minDepth(root -> left) + 1; if(root -> left == NULL && root -> right != NULL) return minDepth(root -> right) + 1; return min(minDepth(root -> left) + 1,minDepth(root -> right) + 1); }};
阅读全文
0 0
- 第十八周:[Leetcode]111. Minimum Depth of Binary Tree
- 第十八周:111. Minimum Depth of Binary Tree
- LeetCode 111. Minimum Depth of Binary Tree
- [LeetCode]111.Minimum Depth of Binary Tree
- 【LeetCode】111.Minimum Depth of Binary Tree
- [Leetcode] 111. Minimum Depth of Binary Tree
- [leetcode] 111. Minimum Depth of Binary Tree
- 111. Minimum Depth of Binary Tree LeetCode
- 【LeetCode】111. Minimum Depth of Binary Tree
- LeetCode *** 111. Minimum Depth of Binary Tree
- LeetCode 111. Minimum Depth of Binary Tree
- leetcode-111. Minimum Depth of Binary Tree
- LeetCode-111.Minimum Depth of Binary Tree
- LeetCode 111.Minimum Depth of Binary Tree
- Leetcode 111. Minimum Depth of Binary Tree
- [leetcode]111.Minimum Depth of Binary Tree
- 111.[Leetcode]Minimum Depth of Binary Tree
- LeetCode 111. Minimum Depth of Binary Tree
- Android 壁纸设置 总结
- 售前项目方案模板
- LeetCode 9. Palindrome Number
- 数据仓库Hive表
- 策略模式
- 第十八周:[Leetcode]111. Minimum Depth of Binary Tree
- 暑期C++之 copy构造函数调用时机
- 漫谈操作系统3 -- 基础知识 (用户态和内核态分离)
- ORA-12514
- 百度地图计算距离
- 点击Android按钮跳转到React-native指定界面
- PB关于字符串按照一定输入格式、规则检测(正则表达式)
- python爬虫step1:selenium
- Docker Ubuntu TLS handshake failed