本问题对应的 leetcode 原文链接:剑指 Offer 32 – II. 从上到下打印二叉树
问题描述
从上到下按层打印二叉树,同一层的节点按从左到右的顺序打印,每一层打印到一行。
例如:
给定二叉树: [3,9,20,null,null,15,7]
,
3
/ \
9 20
/ \
15 7
返回其层次遍历结果:
[
[3],
[9,20],
[15,7]
]
提示:
节点总数 <= 1000
解题思路
视频讲解直达: 本题视频讲解
代码实现
class Solution {
public List<List<Integer>> levelOrder(TreeNode root) {
if(root == null){
return new ArrayList<>();
}
Queue<TreeNode> queue = new LinkedList<>();
List<List<Integer>> res = new ArrayList<>();
queue.add(root);
while(!queue.isEmpty()){
int k = queue.size();
List<Integer> tmp = new ArrayList<>();
for(int i = 0; i < k; i++){
TreeNode t = queue.poll();
tmp.add(t.val);
if(t.left != null) queue.add(t.left);
if(t.right != null) queue.add(t.right);
}
res.add(tmp);
}
return res;
}
}
时间复杂度:O(n)
额外空间复杂度:容器里最对存放 1/2 的节点,故为 O(n)
Python
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
def levelOrder(self, root):
"""
:type root: TreeNode
:rtype: List[List[int]]
"""
if not root:
return []
queue = [root]
res = []
while queue:
size = len(queue)
tmp = []
for i in range(size):
node = queue.pop(0)
tmp.append(node.val)
if node.left:
queue.append(node.left)
if node.right:
queue.append(node.right)
res.append(tmp)
return res
C++
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<vector<int>> levelOrder(TreeNode* root) {
vector<vector<int>> res;
if (!root) {
return res;
}
queue<TreeNode*> q{{root}};
while (!q.empty()) {
int n = q.size();
vector<int> level;
for (int i = 0; i < n; ++i) {
auto t = q.front();
q.pop();
level.push_back(t->val);
if (t->left) {
q.push(t->left);
}
if (t->right) {
q.push(t->right);
}
}
res.push_back(level);
}
return res;
}
};
Go
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func levelOrder(root *TreeNode) [][]int {
if root == nil {
return [][]int{}
}
var res [][]int
queue := []*TreeNode{root}
for len(queue) > 0 {
size := len(queue)
level := []int{}
for i := 0; i < size; i++ {
node := queue[0]
queue = queue[1:]
level = append(level, node.Val)
if node.Left != nil {
queue = append(queue, node.Left)
}
if node.Right != nil {
queue = append(queue, node.Right)
}
}
res = append(res, level)
}
return res
}
JS
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @return {number[][]}
*/
var levelOrder = function(root) {
if (root == null) {
return [];
}
var queue = [];
var res = [];
queue.push(root);
while (queue.length > 0) {
var k = queue.length;
var tmp = [];
for (var i = 0; i < k; i++) {
var t = queue.shift();
tmp.push(t.val);
if (t.left != null) queue.push(t.left);
if (t.right != null) queue.push(t.right);
}
res.push(tmp);
}
return res;
};
评论(1)
时间复杂度on
与上一题基本类似,采用BFS的方法按层打印,这里的每层打印到一行可以将本层全部节点打印到一行,并将下一层全部节点加入队列,以此类推即可分为多行打印,加一个临时列表,然后循环,循环次数是队列的长度,将每一层的数据存入临时列表后然后循环结束,将这一层的再放入res