Skip to content

Commit 80dd434

Browse files
author
jinvicky
committed
max depth solution
1 parent f1cdd1e commit 80dd434

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution {
2+
// ์ตœ๋Œ€ ๊นŠ์ด๋ฅผ ํƒ์ƒ‰ํ•˜๋Š” ๊ฒƒ์ด๋ฏ€๋กœ BFS๋ณด๋‹ค DFS๊ฐ€ ๋” ์ ์ ˆํ•ฉ๋‹ˆ๋‹ค.
3+
// ๋งค ๋…ธ๋“œ์˜ left, right์„ ์žฌ๊ท€๋กœ ํ˜ธ์ถœํ•ด์„œ ์ตœ๋Œ€ ๊นŠ์ด๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ์žฌ๊ท€ ํ•จ์ˆ˜ ํ•„์š”.
4+
public int maxDepth(TreeNode root) {
5+
// ์žฌ๊ท€ ์ข…๋ฃŒ ์กฐ๊ฑด == left ํ˜น์€ right๊ฐ€ null์ผ ๋•Œ, root ์ž์ฒด๊ฐ€ ๋„์ด๋ผ๋ฉด?
6+
if (root == null) return 0;
7+
8+
// ๋ˆ„์ ์„ ์œ„ํ•ด์„œ 1+๋ฅผ ํ•˜๊ณ , Math.max()๋กœ ์ตœ๋Œ“๊ฐ’์„ ๊ตฌํ•ฉ๋‹ˆ๋‹ค.
9+
return 1+ Math.max(maxDepth(root.left), maxDepth(root.right));
10+
}
11+
}

0 commit comments

Comments
ย (0)