(Tree, Easy) Maximum Depth of Binary Tree

송재호·2025년 8월 10일
0

https://leetcode.com/problems/maximum-depth-of-binary-tree/

최대 깊이를 리턴해야 하는 문제다.
재귀로 left, right 돌려서 둘 중 큰 값에 + 1하면 된다.

class Solution {
    public int maxDepth(TreeNode root) {
        if (root == null) return 0;
        return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1;
    }
}
profile
식지 않는 감자

0개의 댓글