"Leetcode-Interview algorithm classic-java implementation" "103-binary tree Zigzag level Order traversal (binary hierarchical Z-zigzag traversal)"

Source: Internet
Author: User

"103-binary tree Zigzag level Order traversal (binary tree layered zigzag traversal)" "leetcode-Interview algorithm classic-java Implementation" "All topics Directory Index" 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 between).
For example:
Given binary Tree {3,9,20,#,#,15,7} ,

    3     9  20    /     15   7

Return its zigzag level order traversal as:

[  [3],  [20,9],  [15,7]]

Main Topic

Given a binary tree, from top to bottom, Z-level traversal, that is: if this layer is left-to-right, the lower is from right to left.

Thinking of solving problems

The binary tree layered traversal is improved using two stacks.

Code Implementation

Tree Node class

publicclass TreeNode {    int val;    TreeNode left;    TreeNode right;    TreeNode(int x) {        val = x;    }}

Algorithm implementation class

ImportJava.Util.Deque;ImportJava.Util.LinkedList;ImportJava.Util.List;/** * Author: Wang Junshu * date:2015-06-23 * time:13:24 * declaration:all rights Reserved!!! */ PublicClass Solution { Public List<List<Integer>>Zigzaglevelorder (TreeNode root) {List<List<Integer>>Result= NewLinkedList<>();if(Root== NULL) {returnResult }//Traversal flag, 0 for left to right, 1 for right-to-leftint flag= 0; TreeNode node;//Record the elements of each layer        List<Integer>Lay= NewLinkedList<>();//bidirectional queue, used as a stack to record the current layer to be processed nodeDeque<TreeNode> Stack = NewLinkedList<>();//Record the next layer of pending nodes.Deque<TreeNode>Nextstack= NewLinkedList<>();Stack.Add (root); while(!Stack.IsEmpty ()) {//delete top of stack elementNode= Stack.Removelast ();//Results QueuedLay.Add (node.Val);//If you are currently traversing from left to right, add in the order of the Zuozi right subtree            if(Flag== 0) {if(node.Left!= NULL) {Nextstack.AddLast (node.left); }if(node.Right!= NULL) {Nextstack.AddLast (node.right); }            }//If you are currently traversing right-to-left, add in the order of the left subtree of the right child tree            Else{if(node.Right!= NULL) {Nextstack.AddLast (node.right); }if(node.Left!= NULL) {Nextstack.AddLast (node.left); }            }//The current layer has been processed            if(Stack.IsEmpty ()) {Deque<TreeNode>Temp=nextstack; Nextstack= Stack;Stack =Temp//mark the direction of the next layer of processingFlag= 1 -Flag//Save the results of this layerResult.Add (lay);//Create a new list to process the results of the next layerLay= NewLinkedList<>(); }        }returnResult }}
Evaluation Results

  Click on the picture, the mouse does not release, drag a position, release after the new window to view the full picture.

Special Instructions Welcome reprint, Reprint please indicate the source "http://blog.csdn.net/derrantcm/article/details/47354341"

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

leetcode-Interview Algorithm classic-java implementation "" 103-binary tree Zigzag level Order traversal (binary tree layered z-zigzag traversal)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.