111. Minimum Depth of Binary Tree

problem

111. Minimum Depth of Binary Tree

 code

/**
 * 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) return 0;
        if(!root->left) return 1+minDepth(root->right);
        if(!root->right) return 1+minDepth(root->left);
        return 1+min(minDepth(root->left), minDepth(root->right));
        
    }
};

 

 

 

 

参考

posted on 2018-11-28 08:56  鹅要长大  阅读(125)  评论(0编辑  收藏  举报

导航