Skip to content
Algorithm.js
📚 단일 패턴

104. Maximum Depth of Binary Tree

재귀 DFS 로 이진 트리의 루트에서 가장 깊은 리프까지의 최대 깊이를 계산하는 방법 정리

Oct 5, 2025 — tree

문제 설명

해결 전략

풀이 아이디어

해결 전략

/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @return {number}
*/
var maxDepth = function(root) {
const dfs = (node) => {
if (!node) return 0;
const left = dfs(node.left);
const right = dfs(node.right);
return Math.max(left, right) + 1;
}
return dfs(root);
};