码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode --- Minimum Depth of Binary Tree

时间:2014-06-08 18:43:48      阅读:220      评论:0      收藏:0      [点我收藏+]

标签:c   style   class   blog   code   java   

题目链接

求二叉树最小深度,最小深度指的是:从根节点走到最近的叶子结点的最短长度

附上代码:

bubuko.com,布布扣
 1 /**
 2  * Definition for binary tree
 3  * struct TreeNode {
 4  *     int val;
 5  *     TreeNode *left;
 6  *     TreeNode *right;
 7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 8  * };
 9  */
10 class Solution {
11 public:
12     int minDepth(TreeNode *root) {
13         if (root == NULL) return 0;
14         if (root->left == NULL && root->right == NULL) return 1;
15         int ans(0x3f3f3f3f);
16         if (root->left != NULL)  ans = minDepth(root->left);
17         if (root->right != NULL) ans = min(ans, minDepth(root->right));
18         return ans + 1;
19     }
20 };
bubuko.com,布布扣

 

LeetCode --- Minimum Depth of Binary Tree,布布扣,bubuko.com

LeetCode --- Minimum Depth of Binary Tree

标签:c   style   class   blog   code   java   

原文地址:http://www.cnblogs.com/Stomach-ache/p/3776092.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!