Skip to content

Commit 74f2553

Browse files
committed
maximum depth of binar tree solution
1 parent 6100b56 commit 74f2553

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// ์‹œ๊ฐ„๋ณต์žก๋„: O(n) ๋ชจ๋“  ๋…ธ๋“œ๋ฅผ ํ•œ๋ฒˆ์”ฉ ๋ฐฉ๋ฌธํ•˜๊ธฐ ๋•Œ๋ฌธ์— n
2+
// ๊ณต๊ฐ„๋ณต์žก๋„: O(n) ์ตœ์•…์˜ ๊ฒฝ์šฐ ๋ชจ๋“  ๋…ธ๋“œ๊ฐ€ ํ•œ์ค„๋กœ ์ด์–ด์ ธ ์žˆ์„ ๋•Œ ์Šคํƒ์— ๋ชจ๋“  ๋…ธ๋“œ๋ฅผ ์ €์žฅํ•ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— n
3+
4+
/**
5+
* Definition for a binary tree node.
6+
* function TreeNode(val, left, right) {
7+
* this.val = (val===undefined ? 0 : val)
8+
* this.left = (left===undefined ? null : left)
9+
* this.right = (right===undefined ? null : right)
10+
* }
11+
*/
12+
/**
13+
* @param {TreeNode} root
14+
* @return {number}
15+
*/
16+
var maxDepth = function (root) {
17+
if (!root) return 0;
18+
19+
return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1;
20+
};
21+
22+
console.log(maxDepth([3, 9, 20, null, null, 15, 7]));
23+
console.log(maxDepth([1, null, 2]));

0 commit comments

Comments
ย (0)