LeetCode : Minimum Depth of Binary Tree
来源:互联网 发布:2016普通网络写手收入 编辑:程序博客网 时间:2024/05/01 15:42
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) { // Start typing your C/C++ solution below // DO NOT write int main() function if(root == NULL){return 0;} if(root->left == NULL && root->right == NULL){ return 1; } int min = 0; int l = -1; int r = -1; if(root->left){ l = minDepth(root->left); } if(root->right){ r = minDepth(root->right); } if(l == -1){ min = r; } else if(r == -1){ min = l; } else{ min = l < r ? l : r; } return min + 1; }};
- 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
- 世界上最有名和哲理的17句话
- Gartner公司数据中心研究部首席分析师张瑾:大数据对数据中心架构的新挑战
- js实现文件上传自定义样式
- jQuery插件jqPlot图表插件——饼图例子
- iOS开发流程总结
- LeetCode : Minimum Depth of Binary Tree
- 关于小米4.0设置静态ip无法保存解决办法?
- 设计模式6 - 适配器模式Adapter
- hibernate也就这些东东
- 地址
- Struts2 配置文件提示
- 呼吸机氧电池的工作原理及性能检测
- 去掉字符串首尾的 空格 换行 回车
- 互联网网站的反爬虫策略浅析