Skip to content

Commit 8174793

Browse files
authored
[ PS ] : Maximum Depth of Binary Tree
1 parent a6e2de7 commit 8174793

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val, left, right) {
4+
* this.val = (val===undefined ? 0 : val)
5+
* this.left = (left===undefined ? null : left)
6+
* this.right = (right===undefined ? null : right)
7+
* }
8+
*/
9+
/**
10+
* ์ฃผ์–ด์ง„ ์ด์ง„ ํŠธ๋ฆฌ์˜ ์ตœ๋Œ€ ๊นŠ์ด๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜
11+
* @param {TreeNode} root
12+
* @return {number}
13+
*/
14+
const maxDepth = function(root) {
15+
let maxDepth = 0;
16+
17+
function bfs(node, depth) {
18+
if (!node) return;
19+
20+
depth += 1;
21+
if (maxDepth < depth) maxDepth = depth;
22+
bfs(node.left, depth);
23+
bfs(node.right, depth);
24+
}
25+
26+
bfs(root, maxDepth);
27+
return maxDepth;
28+
};
29+
30+
// ์‹œ๊ฐ„๋ณต์žก๋„: O(n)
31+
// ๊ณต๊ฐ„๋ณต์žก๋„: O(h) (h: ํŠธ๋ฆฌ์˜ ๋†’์ด. ์ตœ์•…์˜ ๊ฒฝ์šฐ ํŽธํ–ฅํŠธ๋ฆฌ์ผ ๋•Œ h===n์œผ๋กœ O(n))

0 commit comments

Comments
ย (0)