Skip to content

Commit f77974a

Browse files
committed
maximum-depth-of-binary-tree
1 parent 1507910 commit f77974a

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
var maxDepth = function (root) {
2+
let res = 0;
3+
4+
if (!root) return res; // root 자체가 없을 경우만 0
5+
6+
function check(node, depth) {
7+
if (!node.left && !node.right) {
8+
res = Math.max(res, depth);
9+
return;
10+
}
11+
12+
if (node.left) {
13+
check(node.left, depth + 1);
14+
}
15+
if (node.right) {
16+
check(node.right, depth + 1);
17+
}
18+
}
19+
20+
check(root, 1); // 루트부터 시작
21+
return res;
22+
};

0 commit comments

Comments
 (0)