0%

Leetcode103-binaryTreeZigZagLevelOrderTraversal

Description

Given a binary tree, return the zigzag level order traversal of its nodes’ values. (ie, from left to right, then right to left for the next level and alternate between).

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
For example:
Given binary tree [3,9,20,null,null,15,7],
3
/ \
9 20
/ \
15 7
return its zigzag level order traversal as:
[
[3],
[20,9],
[15,7]
]

Solution

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
/**
* 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>> zigzagLevelOrder(TreeNode root) {
List<List<Integer>> res = new ArrayList<>();
if (root == null) return res;

traversal(root, res, 0);

return res;
}
public void traversal(TreeNode node, List<List<Integer>> res, int level){
if (res.size() <= level) res.add(new ArrayList<>());

if (level % 2 == 0) res.get(level).add(node.val);
else res.get(level).add(0, node.val);

if (node.left != null) traversal(node.left, res, level+1);
if (node.right != null) traversal(node.right, res, level+1);
}
}