File tree 1 file changed +28
-0
lines changed
maximum-depth-of-binary-tree
1 file changed +28
-0
lines changed Original file line number Diff line number Diff line change
1
+ package leetcode_study
2
+
3
+ /*
4
+ * ์ด์ง ํธ๋ฆฌ์ ์ต๋ ๊น์ด๋ฅผ ๊ตฌํ๋ ๋ฌธ์
5
+ * ์ฌ๊ท๋ฅผ ์ฌ์ฉํด ๋ฌธ์ ํด๊ฒฐ
6
+ * ์๊ฐ ๋ณต์ก๋: O(n)
7
+ * -> ์ด์ง ํธ๋ฆฌ์ ๋ชจ๋ ๋
ธ๋๋ฅผ ๋ฐฉ๋ฌธ
8
+ * ๊ณต๊ฐ ๋ณต์ก๋: O(n) ํน์ O(log n)
9
+ * -> findDepth() ํจ์๋ ์ฌ๊ท์ ์ผ๋ก ํธ์ถ๋์ด ์ฝ ์คํ์ ์์
10
+ * -> ๊ท ํ์กํ ์ด์งํธ๋ฆฌ์ ๊ฒฝ์ฐ ์ฌ๊ท์ ๊น์ด๋ O(log n) ์์
11
+ * -> ํธํฅ๋ ์ด์งํธ๋ฆฌ์ ๊ฒฝ์ฐ ์ฌ๊ท์ ๊น์ด๋ O(n) ์์
12
+ * */
13
+ fun maxDepth (root : TreeNode ? ): Int {
14
+ if (root == null ) return 0
15
+ val maxValue = findDepth(root, 1 ) // ์์ ๊น์ด ๊ฐ์ `1`
16
+ return maxValue
17
+ }
18
+
19
+ fun findDepth (currentNode : TreeNode ? , depth : Int ): Int {
20
+ // escape condition
21
+ if (currentNode == null ) {
22
+ return depth - 1
23
+ }
24
+
25
+ val leftValue = findDepth(currentNode.left, depth + 1 )
26
+ val rightValue = findDepth(currentNode.right, depth + 1 )
27
+ return maxOf(leftValue, rightValue)
28
+ }
You canโt perform that action at this time.
0 commit comments