LeetCode のminimum-depth-of-binary-tree

Given a binary tree, find its minimum depth.The minimum depth is thenumber of nodes along the shortest path from the root node down to thenearest leaf node.


class Solution {
public:
    int run(TreeNode *root) {
        if(root==NULL)
            return 0;
        if(root->left==NULL)
            return run(root->right)+1;
        if(root->right==NULL)
            return run(root->left)+1;
        
        int left = run(root->left);
        int right = run(root->right);
        
        return left<right?left+1:right+1;
       
    }
};

猜你喜欢

转载自blog.csdn.net/qq_36474990/article/details/80878185