Binary Tree Zigzag level Order traversal

Source: Internet
Author: User

Binary Tree Zigzag level Order traversal

Problem:

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).

Ideas:

Queue Hierarchy Access

My Code:

 Public classSolution { PublicList<list<integer>>Zigzaglevelorder (TreeNode root) {List<List<Integer>> rst =NewArraylist<list<integer>>(); if(Root = =NULL)returnrst; Queue<TreeNode> queue =NewLinkedlist<treenode>();        Queue.offer (root); intCount = 1;  while(!Queue.isempty ()) {            intSize =queue.size (); List<Integer> list =NewArraylist<integer>();  for(inti = 0; i < size; i++) {TreeNode node=Queue.poll (); if(Node.left! =NULL) Queue.offer (node.left); if(Node.right! =NULL) Queue.offer (node.right);            List.add (Node.val); }            if(count%2 = = 1) {rst.add (list); }            Else{collections.reverse (list);            Rst.add (list); } Count++; }        returnrst; }}
View Code

Others code:

 Public classSolution { PublicArraylist<arraylist<integer>>Zigzaglevelorder (TreeNode root) {ArrayList<ArrayList<Integer>> result =NewArraylist<arraylist<integer>>(); if(Root = =NULL) {            returnresult; } Stack<TreeNode> Currlevel =NewStack<treenode>(); Stack<TreeNode> Nextlevel =NewStack<treenode>(); Stack<TreeNode>tmp;        Currlevel.push (root); BooleanNormalorder =true;  while(!Currlevel.isempty ()) {ArrayList<Integer> Currlevelresult =NewArraylist<integer>();  while(!Currlevel.isempty ()) {TreeNode node=Currlevel.pop ();                Currlevelresult.add (Node.val); if(normalorder) {if(Node.left! =NULL) {Nextlevel.push (node.left); }                    if(Node.right! =NULL) {Nextlevel.push (node.right); }                } Else {                    if(Node.right! =NULL) {Nextlevel.push (node.right); }                    if(Node.left! =NULL) {Nextlevel.push (node.left);            }}} result.add (Currlevelresult); TMP=Currlevel; Currlevel=Nextlevel; Nextlevel=tmp; Normalorder= !Normalorder; }        returnresult; }}
View Code

The Learning Place:

    • Although the use of queues can also achieve this problem, but think about, if this layer of the right node is the last element of this layer, then it is the next level of the last access, if the leftmost node of this layer is the last element of the layer access, then the left of the element will be the next layer of access to the node, Therefore, it is more reasonable to use a stack than a queue, and this can save the time of the list rollover under the queue method.

Binary Tree Zigzag level Order 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.