llllmz

导航

111. 二叉树的最小深度c

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */

int min(int i,int j){
    if(i<j) return i;
    return j;
}

int minDepth(struct TreeNode* root) {
    if(!root) return 0;
    if(!root->left && !root->right) return 1;
    int a=INT_MAX;
    int b=INT_MAX;
    if(root->left) a=minDepth(root->left);
    if(root->right) b=minDepth(root->right);
    return min(a,b)+1;
}

 

posted on 2024-03-20 16:49  神奇的萝卜丝  阅读(16)  评论(0)    收藏  举报