Problem
Analysis Process
The most intuitive approach is BFS, which traverses the tree layer by layer.
The default order of BFS at each layer is from left to right, so the BFS algorithm needs to be adjusted to generate the sawtooth sequence
The key is to use a deque traversal, both sides in the queue
If you need to insert elements FIFO (first in first out) the order, then add a new element to the queue tail, after insert element can row behind if need FILO (advanced) after order, then add a new element to the queue first, after the insert element can be at the top
Add the nodes that BFS will access to the queue with a one-layer loop, using a delimiter (for example, an empty node) to separate the nodes of the different layers to indicate the end of one layer and the beginning of a new layer
With the help of double-ended queues, the sawtooth order is implemented at each layer. An empty double-ended queue is used to store all the nodes of the layer. According to the access order of each layer, that is, from left to right or right to left, which end of the double-ended queue should be inserted into the node
Implement a left-to-right traversal order (FIFO) to add elements to the end of the queue to ensure that the added nodes are accessed later
Implement a right-to-left traversal order (FILO) to add elements to the queue header, ensuring that the nodes added later are accessed first
Code
golang
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func zigzagLevelOrder(root *TreeNode) [][]int {
var res [][]int
if root == nil {
return res
}
queue := make([]*TreeNode, 0)
queue = append(queue, root)
isLeftStart := true
for len(queue) > 0 {
l := len(queue)
ans := make([]int, l)
for i := 0; i < l; i++ {
node := queue[i]
if node.Left != nil {
queue = append(queue, node.Left)
}
if node.Right != nil {
queue = append(queue, node.Right)
}
if isLeftStart {
ans[i] = node.Val
} else {
ans[l-i-1] = node.Val
}
}
res = append(res, ans)
isLeftStart = !isLeftStart
queue = queue[l:]
}
return res
}
java
/**
* 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) {
if (root == null) {
return new ArrayList<List<Integer>>();
}
List<List<Integer>> results = new ArrayList<List<Integer>>();
// add the root element with a delimiter to kick off the BFS loop
LinkedList<TreeNode> node_queue = new LinkedList<TreeNode>();
node_queue.addLast(root);
node_queue.addLast(null);
LinkedList<Integer> level_list = new LinkedList<Integer>();
boolean is_order_left = true;
while (node_queue.size() > 0) {
TreeNode curr_node = node_queue.pollFirst();
if (curr_node != null) {
if (is_order_left)
level_list.addLast(curr_node.val);
else
level_list.addFirst(curr_node.val);
if (curr_node.left != null)
node_queue.addLast(curr_node.left);
if (curr_node.right != null)
node_queue.addLast(curr_node.right);
} else {
// we finish the scan of one level
results.add(level_list);
level_list = new LinkedList<Integer>();
// prepare for the next level
if (node_queue.size() > 0)
node_queue.addLast(null);
is_order_left = !is_order_left;
}
}
return results;
}
}