求出一个二叉树的最大深度

package class06;

/**
 * 求出一个二叉树的最大深度
 */
public class Code04_MaximumDepthOfBinaryTree {
    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
    }

    public static int maxDepth(TreeNode head) {
        if (head == null) {
            return 0;
        }
        //获取左子树和右子树较大的深度,再加一。
        return Math.max(maxDepth(head.left), maxDepth(head.right)) + 1;
    }
}

 

posted @ 2022-08-07 17:06  TheFloorIsNotTooHot  阅读(27)  评论(0)    收藏  举报