[LeetCode] 102. Binary Tree Level Order Traversal

Given the root of a binary tree, return the level order traversal of its nodes’ values. (i.e., from left to right, level by level).

Example 1:
Example 1
Input: root = [3,9,20,null,null,15,7]
Output: [[3],[9,20],[15,7]]

Example 2:
Input: root = [1]
Output: [[1]]

Example 3:
Input: root = []
Output: []

Constraints:
The number of nodes in the tree is in the range [0, 2000].
-1000 <= Node.val <= 1000

二叉树的层序遍历。

给你二叉树的根节点 root ,返回其节点值的 层序遍历 。 (即逐层地,从左到右访问所有节点)。

思路

此题比较简单的做法是用层序遍历BFS,但是此题也可以用DFS深度遍历做,比较巧妙。两种思路的时间复杂度是O(n),空间复杂度是O(n^2),这个空间是返回二维 list 所需的空间。

复杂度

时间O(n)
空间O(n^2)

BFS

Java实现

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
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> res = new ArrayList<>();
// corner case
if (root == null) {
return res;
}
// normal case
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);
while (!queue.isEmpty()) {
int size = queue.size();
List<Integer> list = new ArrayList<>();
for (int i = 0; i < size; i++) {
TreeNode cur = queue.poll();
list.add(cur.val);
if (cur.left != null) {
queue.offer(cur.left);
}
if (cur.right != null) {
queue.offer(cur.right);
}
}
res.add(list);
}
return res;
}
}

JavaScript实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/**
* @param {TreeNode} root
* @return {number[][]}
*/
var levelOrder = function(root) {
let res = [];
if (root === null) return res;

// normal case
let queue = [root];
while (queue.length) {
let list = [];
let size = queue.length;
for (let i = 0; i < size; i++) {
let cur = queue.shift();
list.push(cur.val);
if (cur.left !== null) queue.push(cur.left);
if (cur.right !== null) queue.push(cur.right);
}
res.push(list);
}
return res;
};

DFS

DFS的思路的要点在于,需要在递归函数中多一个参数 level,记录当前递归到树的第几层了,同时这个level也决定了最后的结果集里面有几个 subarray。跑一下例子好了。当第一次把根节点3放进res之后,下面就开始遍历他的两个孩子,此时level是1。遍历到左孩子9的时候,level是1,大于等于res.length(1),所以需要再加入一个subarray(15行)以便于加入9这个节点(17行)。当遍历右孩子20的时候,level依然是1,并不大于等于res.length(2),所以此时并不需要再加入subarray了。但是20依然可以被放进最后的结果集。简而言之,DFS用了一个level参数来判断是否大于结果集此时的长度,以决定是否需要再添加新的subarray来存放下一层的节点值。

JavaScript实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/**
* @param {TreeNode} root
* @return {number[][]}
*/
var levelOrder = function(root) {
let res = [];
if (root === null) return res;
helper(res, root, 0);
return res;
};

var helper = function(res, root, level) {
if (root === null) return;
if (level >= res.length) {
res.push([]);
}
res[level].push(root.val);
helper(res, root.left, level + 1);
helper(res, root.right, level + 1);
};

相关题目

1
2
3
102. Binary Tree Level Order Traversal
107. Binary Tree Level Order Traversal II
429. N-ary Tree Level Order Traversal

[LeetCode] 102. Binary Tree Level Order Traversal
https://shurui91.github.io/posts/3757411780.html
Author
Aaron Liu
Posted on
January 12, 2020
Licensed under