Leetcode 111. 二叉树的最小深度

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

你可能感兴趣的:(LeetCode)