https://app.laicode.io/app/problem/58
Get the list of keys in a given binary tree layer by layer in zig-zag order.
Examples
5
/ \
3 8
/ \ \
1 4 11
the result is [5, 3, 8, 11, 4, 1]
Corner Cases
- What if the binary tree is null? Return an empty list in this case.
How is the binary tree represented?
We use the level order traversal sequence with a special symbol "#" denoting the null node.
For Example:
The sequence [1, 2, 3, #, #, 4] represents the following binary tree:
1
/ \
2 3
/
4
Medium
Binary Tree
Breadth First Search
The tree should at least have a root.
This is a modification of Binary Tree Level Order Traversal
Instead of using a queue, we need to use a deque for this one. Take the first tree for example.
5 level 1
/ \
3 8 level 2
/ \ \
1 4 11 level 3
We should poll and offer the nodes to the deque in different directions for odd and even levels such that the poll/offer operation will not mess up what the deque already has.
- When we are at level 1, we need to add 3 and 8 to the deque, so the deque is {3, 8} at the moment.
- At level 2, because we are supposed to poll 3 out of the deque first ⇒ we need a way to do pollFirst() for even levels.
- level % 2 == 0 ⇒ pollFirst()
- after polling 3 out of the deque, the deque is {8}
- the designated ordering of level 3 is [11, 4, 1]. Because of step a, we have level % 2 != 0 ⇒ pollLast() ⇒ 11 should be at the end of the deque ⇒ 1 should be added towards the head of the deque
- if left child != null ⇒ deque.offerLast(left child)
- if right child != null ⇒ deque.offerLast(right child)
- At level 3 ⇒ level % 2 != 0 ⇒ do pollLast()
- if any of the nodes in level 3 has children, to maintain the designated order and avoid messing up the other nodes left in the current level
- deque = {1, 4, 11} at the beginning
- if 11 has both left and right children
- level four should be in order [left child, right child]
- add right child, then left child to the head
- deque = {left, right, 1, 4} ⇒ pollLast() still works
- if any of the nodes in level 3 has children, to maintain the designated order and avoid messing up the other nodes left in the current level
/**
* public class TreeNode {
* public int key;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int key) {
* this.key = key;
* }
* }
*/
public class Solution {
public List<Integer> zigZag(TreeNode root) {
// Write your solution here
List<Integer> result = new ArrayList<>();
if (root == null) {
return result;
}
Deque<TreeNode> deque = new ArrayDeque<>();
deque.offerFirst(root);
int level = 0;
while (!deque.isEmpty()) {
// We are entering a new level
level++;
// This is still a variation of level-order traversal
int size = deque.size();
for (int i = 0; i < size; i++) {
// 1. Even level: pollFirst() and offerLast()
// 2. Odd level: pollLast() and offerFirst()
TreeNode node = level % 2 == 0 ? deque.pollFirst() :
deque.pollLast();
result.add(node.key);
if (level % 2 != 0) {
// In odd level
// Because we do pollLast() to get the nodes from deque
// we need to offerFirst() the right child then left child
// to maintain the order and not mess up polling other nodes
// in the current level
if (node.right != null) {
deque.offerFirst(node.right);
}
if (node.left != null) {
deque.offerFirst(node.left);
}
} else {
// In even level
// Because we do pollFirst() to get the nodes from deque
// we need to offerLast() the left child then right child
// to maintain the order and not mess up polling other nodes
// in the current level
if (node.left != null) {
deque.offerLast(node.left);
}
if (node.right != null) {
deque.offerLast(node.right);
}
}
}
}
return result;
}
}
Time:
Traversing the whole tree means checking every single node in it ⇒ O(n)
Space:
A deque is used in the process ⇒ O(n)