Given a binary tree, returnZigzag level orderTraversal of its nodes 'values. (ie, from left to right, then right to left for the next level and alternate ).
For example:
Given Binary Tree{3,9,20,#,#,15,7},
3 / 9 20 / 15 7
Return its zigzag level order traversal:
[ [3], [20,9], [15,7]]
Question: first, set a variable zigzag to indicate whether reverse order is required for the next layer. If the variable zigzag is set to true, the next layer needs reverse order. Otherwise, no reverse order is required for the next layer. If reverse order is not required, the right child of the node on this layer is first written into the stack before the nextlevel is generated. If the next layer needs reverse order, first, add the left sub of the node to the stack, and then the right sub into the stack.
For example, if zigzag is initially set to true (the second layer requires reverse order), when traversing the first layer (Root Node 3), first write 9 to the stack and then 20 to the stack, then, the L2 traversal sequence obtained at the end of the stack is [20, 9], and then zigzag = false (the L3 does not need to be in reverse order). When traversing the L2, traverse 20 first, add the right sub-stack of 20, then put the right sub-7 of 20 into nextlevel, and then put the left sub-15 into nextlevel. Then, when the third layer exits the stack, the actual result is [].
Here, we need to note that we still need to use stacks without reverse order? Although no reverse order is required for the current layer, the previous layer of the current layer is traversed in reverse order, so Stack is still required. For example, if we add a node to the above tree to the tree shown in:
3 / 9 20 / / 4 15 7
Traverse the third layer should get [, 7], but traverse the second layer in the order of 20, 9, so the 20 children still need to press to the bottom of the stack, in order to ensure that they are behind 4 on the left of 9 when they are popped up.
The Code is as follows:
1 /** 2 * Definition for binary tree 3 * public class TreeNode { 4 * int val; 5 * TreeNode left; 6 * TreeNode right; 7 * TreeNode(int x) { val = x; } 8 * } 9 */10 public class Solution {11 public List<List<Integer>> zigzagLevelOrder(TreeNode root) {12 List<List<Integer>> answer = new ArrayList<List<Integer>>();13 if(root == null)14 return answer;15 16 Stack<TreeNode> currLevel = new Stack<TreeNode>();17 Stack<TreeNode> nextLevel = new Stack<TreeNode>();18 boolean zigZag = true;19 20 currLevel.push(root);21 while(!currLevel.isEmpty()){22 ArrayList<Integer> result = new ArrayList<Integer>();23 24 while(!currLevel.isEmpty()){25 TreeNode node = currLevel.pop();26 result.add(node.val);27 28 if(!zigZag){29 if(node.right != null)30 nextLevel.push(node.right);31 if(node.left != null)32 nextLevel.push(node.left);33 }34 else {35 if(node.left != null)36 nextLevel.push(node.left);37 if(node.right != null)38 nextLevel.push(node.right);39 }40 }41 42 zigZag = !zigZag;43 List<Integer> temp = new ArrayList<Integer>(result);44 answer.add(temp);45 result.clear();46 Stack<TreeNode> tmp = new Stack<TreeNode>();47 tmp.addAll(nextLevel);48 currLevel = tmp;49 nextLevel.clear();50 }51 return answer;52 }53 }
In the code above, the row 46 stack TMP is used to save the space of the nextlevel stack. Otherwise, although the nextlevel stack is assigned to the currlevel, when the nextlevel is cleared, this part of memory will also be cleared, so the elements in nextlevel should be placed in another memory.
Stack constructor can not directly initialize the stack into another stack, there are many ways to assign a stack to another stack, you can refer to here: http://stackoverflow.com/questions/7919836/how-do-i-copy-a-stack-in-java