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

Minimum Depth of Binary Tree 【leetcode】

时间:2015-07-22 22:40:01      阅读:103      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!