首页 > 其他 > 详细

Minimum Depth of Binary Tree 【leetcode】

时间:2015-07-22 22:37:48      阅读:251      评论:0      收藏:0      [点我收藏+]
/**
 * Definition for a binary tree node.
 * 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;
        dep(root,0);
    }
    
    int dep(TreeNode* root,int l){
        if(root==NULL) return INT_MAX;
        if(root -> left ==NULL && root->right ==NULL) return l+1;
        return min(dep(root->left,l+1),dep(root->right,l+1));
    }
};

注意题目要求,必须是到叶子结点。

Minimum Depth of Binary Tree 【leetcode】

原文:http://www.cnblogs.com/julie-yang/p/4668725.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!