-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathProblem107.js
42 lines (36 loc) · 855 Bytes
/
Problem107.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// Problem 107
//
// This problem was asked by Microsoft.
//
// Print the nodes in a binary tree level-wise. For example, the following should print 1, 2, 3, 4, 5.
//
// 1
// / \
// 2 3
// / \
// 4 5
//
// https://leetcode.com/problems/binary-tree-level-order-traversal/
//
// O(N) Time complexity
// O(N) Space complexity
// N is the number of nodes in the tree
/**
* Returns the list of nodes in a binary tree level wise
* @param {TreeNode} root
* @return {number[]}
*/
function levelOrder(root) {
const list = [];
if (root === null) return list;
const queue = [];
queue.push(root);
while (queue.length !== 0) {
const node = queue.shift();
list.push(node.val);
if (node.left !== null) queue.push(node.left);
if (node.right !== null) queue.push(node.right);
}
return list;
}
export default levelOrder;