-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathNo102.binary-tree-level-order-traversal.js
83 lines (80 loc) · 1.78 KB
/
No102.binary-tree-level-order-traversal.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/**
* Difficulty:
* Medium
*
* Desc:
* Given a binary tree, return the level order traversal of its nodes' values.
* (ie, from left to right, level by level).
*
* Example:
* Given binary tree [3,9,20,null,null,15,7],
3
/ \
9 20
/ \
15 7
* return its level order traversal as:
* [
* [3],
* [9,20],
* [15,7]
* ]
*
* 层序遍历,一次遍历树的一层
*/
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/* ============================ Recursive Solution ============================ */
/**
* @param {TreeNode} root
* @return {number[][]}
*/
var levelOrder = function(root) {
const results = [];
if (!root) return results;
const getLayerValues = (nodes) => {
const childs = [];
const values = [];
for (let i = 0; i < nodes.length; i += 1) {
const node = nodes[i];
values.push(node.val);
if (node.left) childs.push(node.left);
if (node.right) childs.push(node.right);
}
results.push(values);
if (childs.length) getLayerValues(childs);
};
getLayerValues([root]);
return results;
};
/* ============================ Iteratively Solution ============================ */
/**
* @param {TreeNode} root
* @return {number[][]}
*/
const levelOrder_iteratively = (root) => {
const result = [];
let nodes = [root];
let tmp = [];
let tmpVal = [];
let layer = 0;
while (nodes.length) {
const node = nodes.shift();
node && tmpVal.push(node.val);
node && node.left && tmp.push(node.left);
node && node.right && tmp.push(node.right);
if (!nodes.length && tmpVal.length) {
nodes = tmp;
result[layer] = tmpVal;
layer += 1;
tmpVal = [];
tmp = [];
}
}
return result;
};