depth
: 최대 깊이dfs
: 깊이 우선탐색, 인자로 현재 조회중인 노드(node
)와 해당 노드의 깊이(curDepth
)를 갖음dfs
를 root
로 부터 1
의 깊이로 수행null
이라면, 탐색 종료depth
에 현재 깊이와 탐색된 최대 깊이 비교하여 할당leaf
노드들을 순회하며 깊이를 추가한 상태로 탐색depth
) 반환function maxDepth(root: _Node | null): number {
let depth = 0
function dfs(node: _Node | null, curDepth: number) {
if(!node) return
depth = Math.max(depth, curDepth)
for(const curNode of node.children) dfs(curNode, curDepth + 1)
}
dfs(root, 1)
return depth
};