File tree 1 file changed +30
-0
lines changed
maximum-depth-of-binary-tree
1 file changed +30
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
You canโt perform that action at this time.
0 commit comments