0
点赞
收藏
分享

微信扫一扫

LeetCode-111-二叉树的最小深度

酷子腿长一米八 2021-09-28 阅读 200

二叉树的最小深度

解法一:递归
public class LeetCode_111 {
    public static int result = Integer.MAX_VALUE;

    public static int minDepth(TreeNode root) {
        if (root == null) {
            return 0;
        }
        minDepth(root, 0);
        return result;
    }

    public static void minDepth(TreeNode root, int curDepth) {
        if (root == null) {
            return;
        }
        curDepth++;
        if (root.left == null && root.right == null) {
            if (curDepth < result) {
                result = curDepth;
            }
            return;
        }
        if (root.left == null && root.right != null) {
            minDepth(root.right, curDepth);
            return;
        }
        if (root.left != null && root.right == null) {
            minDepth(root.left, curDepth);
            return;
        }
        minDepth(root.left, curDepth);
        minDepth(root.right, curDepth);
    }

    public static void main(String[] args) {
        TreeNode root = new TreeNode(2);
        root.right = new TreeNode(3);
        root.right.right = new TreeNode(4);
        root.right.right.right = new TreeNode(5);
        root.right.right.right.right = new TreeNode(6);
        System.out.println(minDepth(root));
    }
}
举报

相关推荐

0 条评论