【leetcode】111.(Easy)Minimum Depth of Binary Tree

解题思路:
递归


提交代码:

class Solution {
    public int minDepth(TreeNode root) {
        if(root==null)	return 0;
        if(root.left==null&&root.right==null)	return 1;
        
        if(root.left==null)	return 1+minDepth(root.right);
        if(root.right==null)	return 1+minDepth(root.left);
        
        int left=minDepth(root.left);
        int right=minDepth(root.right);
        return Math.min(left, right)+1;
    }
}

运行结果:

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/AXIMI/article/details/85373744