Skip to content

Commit f806b49

Browse files
feat: maximum depth of binary tree 풀이
1 parent b5c7975 commit f806b49

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+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val, left, right) {
4+
* this.val = (val===undefined ? 0 : val)
5+
* this.left = (left===undefined ? null : left)
6+
* this.right = (right===undefined ? null : right)
7+
* }
8+
*/
9+
/**
10+
* @param {TreeNode} root
11+
* @return {number}
12+
*/
13+
var maxDepth = function (root) {
14+
if (!root) return 0;
15+
16+
// 왼쪽과 오른쪽 서브트리의 최대 깊이를 구한다.
17+
const leftDepth = maxDepth(root.left);
18+
const rightDepth = maxDepth(root.right);
19+
20+
// 현재 노드의 깊이는 왼쪽과 오른쪽 깊이 중 큰 값에 1을 더한 값이다.
21+
return Math.max(leftDepth, rightDepth) + 1;
22+
};

0 commit comments

Comments
 (0)