【easy】111. Minimum Depth of Binary Tree求二叉树的最小深度

求二叉树的最小深度:

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

    }
};

 

posted @ 2018-01-17 10:13  Sherry_Yang  阅读(120)  评论(0编辑  收藏  举报