leetCode:Minimum Depth of Binary Tree
来源:互联网 发布:mac版本spss 编辑:程序博客网 时间:2024/05/17 02: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.
#include<iostream>#include<algorithm>using namespace std;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; return min(1+minDepth(root->left),1+minDepth(root->right)); }};int main() { TreeNode a(5); TreeNode b(4); TreeNode c(8); TreeNode d(11); TreeNode e(13); TreeNode f(4); TreeNode g(7); TreeNode h(2); TreeNode i(1); a.left=&b;a.right=&c; b.left=&d;b.right=NULL; c.left=&e;c.right=&f; d.left=&g;d.right=&h; e.left=NULL;e.right=NULL; f.left=NULL;f.right=&i; g.left=NULL;g.right=NULL; h.left=NULL;h.right=NULL; i.left=NULL;i.right=NULL; Solution sol; int result=sol.minDepth(&a); cout<< result<<endl; }
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
- 带...参数的函数定义及使用
- phantomjs + casperjs 抓取网页截图
- 仙剑四、五、五前模型及资源提取
- TF-IDF
- Ubuntu重装mysql
- leetCode:Minimum Depth of Binary Tree
- HDU-4991-Ordered Subsequence
- 数据结构之排序算法整理(1)
- PowerDesigner 15破解版下载
- 每天一个linux命令(16):which命令
- 黑马程序员-小框架的实现
- 调试成功:server 及 zhujiemian1
- 每天一个linux命令(17):whereis 命令
- 文件传输 个人