Skip to content

Commit e938765

Browse files
committed
maximum depth of binary tree solution
1 parent 1ab1e6f commit e938765

1 file changed

Lines changed: 27 additions & 0 deletions

File tree

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
์ด ๋ฌธ์ œ๋ฅผ ํ‘ธ๋Š” ํ•ต์‹ฌ์€ DFS(๊นŠ์ด ์šฐ์„  ํƒ์ƒ‰)๋‹ค.
3+
์™ผ์ชฝ์œผ๋กœ, ์˜ค๋ฅธ์ชฝ์œผ๋กœ ์ญ‰ ๋“ค์–ด๊ฐ€๋Š” ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•ด์„œ ๋ณ€์ˆ˜์— ์ €์žฅํ•œ๋‹ค.
4+
์ด๋ฅผ ์žฌ๊ท€ํ˜ธ์ถœํ•˜๋ฉด ์‰ฝ๊ฒŒ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋‹ค.
5+
*/
6+
/**
7+
* Definition for a binary tree node.
8+
* function TreeNode(val, left, right) {
9+
* this.val = (val===undefined ? 0 : val)
10+
* this.left = (left===undefined ? null : left)
11+
* this.right = (right===undefined ? null : right)
12+
* }
13+
*/
14+
/**
15+
* @param {TreeNode} root
16+
* @return {number}
17+
*/
18+
function maxDepth(root) {
19+
if (root === null) return 0;
20+
21+
let left_depth = maxDepth(root.left);
22+
let right_depth = maxDepth(root.right);
23+
24+
let count = Math.max(left_depth, right_depth) + 1;
25+
26+
return count;
27+
}

0 commit comments

Comments
ย (0)