请实现一个函数按照之字形顺序打印二叉树,即第一行按照从左到右的顺序打印,第二层按照从右到左的顺序打印,第三行再按照从左到右的顺序打印,其他行以此类推。
例如:
给定二叉树: [3,9,20,null,null,15,7]
,
3
/ \
9 20
/ \
15 7
返回其层次遍历结果:
[
[3],
[20,9],
[15,7]
]
提示:
节点总数 <= 1000
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def levelOrder(self, root: TreeNode) -> List[List[int]]:
if root is None:
return []
q = deque()
res = []
q.append(root)
while q:
size = len(q)
t = []
for _ in range(size):
node = q.popleft()
t.append(node.val)
if node.left is not None:
q.append(node.left)
if node.right is not None:
q.append(node.right)
res.append(t if len(res) & 1 == 0 else t[::-1])
return res
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public List<List<Integer>> levelOrder(TreeNode root) {
if (root == null) return Collections.emptyList();
Deque<TreeNode> q = new ArrayDeque<>();
List<List<Integer>> res = new ArrayList<>();
q.offer(root);
while (!q.isEmpty()) {
int size = q.size();
List<Integer> t = new ArrayList<>();
while (size-- > 0) {
TreeNode node = q.poll();
t.add(node.val);
if (node.left != null) q.offer(node.left);
if (node.right != null) q.offer(node.right);
}
if ((res.size() & 1) == 1) Collections.reverse(t);
res.add(t);
}
return res;
}
}
/**
* 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) return [];
let queue = [root];
let res = [];
let depth = 0;
let dir = true;
while (queue.length) {
let len = queue.length;
for (let i = 0; i < len; i++) {
let node = queue.shift();
if (!node) continue;
if (!res[depth]) res[depth] = [];
if (dir) {
res[depth].push(node.val);
} else {
res[depth].unshift(node.val);
}
queue.push(node.left, node.right);
}
depth++;
dir = !dir;
}
return res;
};
func levelOrder(root *TreeNode) [][]int {
if root == nil {
return nil
}
res := [][]int{}
queue := []*TreeNode{}
queue = append(queue,root)
level := 0
for len(queue) != 0 {
size := len(queue)
ans := []int{}
//size记录每层大小,level记录层数
for size > 0 {
cur := queue[0]
if level & 1 == 0 {
ans = append(ans, cur.Val)
} else {
ans = append([]int{cur.Val},ans...)
}
queue = queue[1:]
size--
if cur.Left != nil {
queue = append(queue, cur.Left)
}
if cur.Right != nil {
queue = append(queue, cur.Right)
}
}
level++
res = append(res, ans)
}
return res
}
class Solution {
public:
vector<vector<int>> levelOrder(TreeNode* root) {
vector<vector<int>> ans;
if (root == NULL) return ans;
queue<TreeNode*> q;
q.push(root);
bool flag = false;
while (!q.empty()) {
int n = q.size();
vector<int> v;
for (int i = 0; i < n; ++i) {
TreeNode* node = q.front();
q.pop();
v.emplace_back(node->val);
if (node->left) q.push(node->left);
if (node->right) q.push(node->right);
}
if (flag) reverse(v.begin(), v.end());
flag = !flag;
ans.emplace_back(v);
}
return ans;
}
};