Skip to content

Commit db7585b

Browse files
committed
add solution : 104. Maximum Depth of Binary Tree
1 parent 687e199 commit db7585b

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class TreeNode {
2+
val: number;
3+
left: TreeNode | null;
4+
right: TreeNode | null;
5+
constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
6+
this.val = val === undefined ? 0 : val;
7+
this.left = left === undefined ? null : left;
8+
this.right = right === undefined ? null : right;
9+
}
10+
}
11+
12+
/**
13+
* @link https://leetcode.com/problems/maximum-depth-of-binary-tree/description/
14+
*
15+
* ์ ‘๊ทผ ๋ฐฉ๋ฒ• : DFS ์‚ฌ์šฉ
16+
* - ๊ฐ ๋…ธ๋“œ์—์„œ ์™ผ์ชฝ ์„œ๋ธŒํŠธ๋ฆฌ์™€ ์˜ค๋ฅธ์ชฝ ์„œ๋ธŒํŠธ๋ฆฌ ๊นŠ์ด ๊ณ„์‚ฐํ•œ ํ›„, ๋” ๊นŠ์€ ๊ฐ’์— 1 ๋”ํ•˜๊ธฐ
17+
* - ์ข…๋ฃŒ ์กฐ๊ฑด : ๋…ธ๋“œ๊ฐ€ null์ผ ๋•Œ 0 ๋ฐ˜ํ™˜
18+
*
19+
* ์‹œ๊ฐ„๋ณต์žก๋„ : O(n)
20+
* - n = ํŠธ๋ฆฌ์˜ ๋…ธ๋“œ ๊ฐœ์ˆ˜
21+
* - ๋…ธ๋“œ ํ•œ ๋ฒˆ์”ฉ ๋ฐฉ๋ฌธํ•ด์„œ ๊นŠ์ด ๊ณ„์‚ฐ
22+
*
23+
* ๊ณต๊ฐ„๋ณต์žก๋„ : O(n)
24+
* - ๊ธฐ์šธ์–ด์ง„ ํŠธ๋ฆฌ์˜ ๊ฒฝ์šฐ, ํŠธ๋ฆฌ ์ตœ๋Œ€ ๊นŠ์ด๋งŒํผ ์žฌ๊ท€ ํ˜ธ์ถœ ์Šคํƒ ์Œ“์ž„
25+
*/
26+
function maxDepth(root: TreeNode | null): number {
27+
if (!root) return 0;
28+
29+
return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1;
30+
}

0 commit comments

Comments
ย (0)