Skip to content

Commit 7b641d3

Browse files
committed
merge depth solution
1 parent 8d5456c commit 7b641d3

1 file changed

Lines changed: 34 additions & 0 deletions

File tree

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
* 문제: https://leetcode.com/problems/maximum-depth-of-binary-tree/
10+
* 요구사항: 이진 트리의 최대 깊이를 반환하라.
11+
*/
12+
/**
13+
* @param {TreeNode} root
14+
* @return {number}
15+
*/
16+
const maxDepth = (root) => {
17+
if(!root) return 0;
18+
19+
let queue = [root];
20+
let depth = 0;
21+
22+
while(queue.length) {
23+
let size = queue.length;
24+
25+
for(let i = 0; i < size; i++) {
26+
let node = queue.shift();
27+
28+
if(node.left) queue.push(node.left);
29+
if(node.right) queue.push(node.right);
30+
}
31+
depth++;
32+
}
33+
return depth;
34+
};

0 commit comments

Comments
 (0)