您的位置:首页 > 编程语言 > C语言/C++

leetcode 日经贴,Cpp code -Minimum Depth of Binary Tree

2015-04-21 17:11 465 查看
Minimum Depth of Binary Tree

/**
* 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) {
return 0;
}
if (!root->left && !root->right) {
return 1;
}
int a = INT_MAX, b = INT_MAX;
if (root->left) {
a = minDepth(root->left) + 1;
}
if (root->right) {
b = minDepth(root->right) + 1;
}
return min(a, b);
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: