W
e
l
c
o
m
e
: )

每日一题:二叉树的最大深度问题

/**
 * 返回一棵树的最大深度、
 * 测试链接:https://leetcode.com/problems/maximum-depth-of-binary-tree
 */
public class Code03_MaximumDepthOfBinaryTree {
    public static class TreeNode {
        public int val;
        public TreeNode left;
        public TreeNode right;
    }

    public static int maxDepth(TreeNode root){
        if (root == null){
            return 0;
        }
        return Math.max(maxDepth(root.left),maxDepth(root.right)) + 1;
    }
}
posted @ 2026-08-02 09:18  寒月静无光  阅读(2)  评论(0)    收藏  举报