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

leetcode-Minimum Depth of Binary Tree(2014.1.22)

2014-04-15 19:55 381 查看
/**
 * 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==NULL) return 0;
        if(root->left==NULL&&root->right==NULL) return 1;
        int leftDepth=minDepth(root->left);
        int rightDepth=minDepth(root->right);
        if(leftDepth==0){
            return rightDepth+1;
        } else if(rightDepth==0){
            return leftDepth+1;
        }
        return min(leftDepth,rightDepth)+1;
    }
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  leetcode 编程