[LeetCode-interview algorithm classic-Java implementation] [103-Binary Tree Zigzag Level Order Traversal (Binary Tree layered Z-shaped Traversal)], zigzagleetcode
[103-Binary Tree Zigzag Level Order Traversal (Binary Tree layered Z-shaped Traversal )][LeetCode-interview algorithm classic-Java implementation] [directory indexes for all questions]Original question
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 ).
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]]
Theme
Given a binary tree, perform layered Z-shaped traversal from top to bottom. That is, if the layer is left to right, the lower layer is left to right.
Solutions
Improve the layered traversal of Binary Trees by using two stacks.
Code Implementation
Tree node class
public class TreeNode { int val; TreeNode left; TreeNode right; TreeNode(int x) { val = x; }}
Algorithm Implementation class
Import java. util. deque; import java. util. using list; import java. util. list;/*** Author: Wang junchao * Date: 2015-06-23 * Time: * Declaration: All Rights Reserved !!! */Public class Solution {public List <Integer> zigzagLevelOrder (TreeNode root) {List <Integer> result = new writable List <> (); if (root = null) {return result;} // indicates the traversal flag. 0 indicates from left to right, 1 indicates from right to left int flag = 0; TreeNode node; // record the element List of each layer <Integer> lay = new queue List <> (); // bidirectional queue, used as a stack, record the Deque <TreeNode> stack = new record list <> (); // record the Deque <TreeNode> nextStack = new Linked List <> (); stack. add (root); while (! Stack. isEmpty () {// Delete the stack top element node = stack. removeLast (); // The result is queued for lay. add (node. val); // if the current traversal is from left to right, add if (flag = 0) {if (node. left! = Null) {nextStack. addLast (node. left) ;}if (node. right! = Null) {nextStack. addLast (node. right) ;}}// if the current traversal is from right to left, add else {if (node. right! = Null) {nextStack. addLast (node. right) ;}if (node. left! = Null) {nextStack. addLast (node. left) ;}} // The current layer has finished processing if (stack. isEmpty () {Deque <TreeNode> temp = nextStack; nextStack = stack; stack = temp; // flag the processing direction of the next layer. flag = 1-flag; // Save the result of this layer. add (lay); // create a new linked list to process the results of the next layer lay = new linked list <> () ;}} return result ;}}
Evaluation Result
Click the image. If you do not release the image, drag it to a position. After the image is released, you can view the complete image in the new window.
Note
Please refer to the following link for more information: http://blog.csdn.net/derrantcm/article/details/47354341]
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.