Skip to content

Commit 86be455

Browse files
committed
maximum depth of binary tree
1 parent 9cb7c0b commit 86be455

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* Runtime: 0ms
3+
* Time Complexity: O(n)
4+
*
5+
* Memory: 44.28MB
6+
* Space Complexity: O(h)
7+
* - h: ํŠธ๋ฆฌ์˜ ๋†’์ด or ์ตœ๋Œ€ ๊นŠ์ด
8+
*
9+
* Approach: ์žฌ๊ท€๋ฅผ ์ด์šฉํ•œ DFS ์ ‘๊ทผ๋ฒ•
10+
* - ๋ฃจํŠธ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜์—ฌ ์žฌ๊ท€๊ฐ€ ์‹œ์ž‘ํ•  ๋•Œ๋งˆ๋‹ค 1๋กœ ๊ณ„์‚ฐ
11+
* - ํŠธ๋ฆฌ์˜ ๋์— ๋„๋‹ฌํ•˜๋ฉด 0์„ ๋ฆฌํ„ดํ•˜์—ฌ ์ฐจ๋ก€๋Œ€๋กœ ๋‚จ์€ ๊นŠ์ด๋ฅผ ๊ณ„์‚ฐ
12+
*/
13+
class Solution {
14+
public int maxDepth(TreeNode root) {
15+
if (root == null)
16+
return 0;
17+
18+
return 1 + Math.max(maxDepth(root.left), maxDepth(root.right));
19+
}
20+
}

0 commit comments

Comments
ย (0)