Print binary tree in zigzag order

Source: Internet
Author: User

Title Description: Print binary tree in zigzag order
Implement a function to print a binary tree by its glyph, that is, the first line is printed from left to right, the second layer is printed from right to left, the third line is printed from left to right, and so on in the other rows.

Analytical:
To layer a print, you cannot use the normal method to access a node and then press the left and right child nodes of the node into the queue.
Odd layers print from left to right, and even layers print from right to left. Use two stacks to store nodes for each layer.

    • When the odd layer is accessed , the sub-nodes of the odd layer are pressed into the stack of the even layer in the order of first left and right .
    • When the even layer is accessed , the sub-nodes of the even layer are pressed into the stack of odd layers in the order of first right and left .
/*struct TreeNode {int val;    struct TreeNode *left;    struct TreeNode *right; TreeNode (int x): Val (x), left (null), right (null) {}};*/classSolution { Public: vector<vector<int> >Print (treenode* proot) { vector<vector<int> >Resultif(Proot = = NULL)returnResult Stack<TreeNode*>STK1, STK2; Stk1.push (Proot);//STK1 storage odd layer; STK2 storage even layer         while(!stk1.empty () | |!stk2.empty ()) { vector<int>Level_vec;if(!stk1.empty ()) { while(!stk1.empty ())                    {treenode* ptop = Stk1.top (); Level_vec.push_back (Ptop->val);if(Ptop->left! = NULL)//Stack first left and right, right-to-left (even layer) when out of stackStk2.push (Ptop->left);if(Ptop->right! = NULL) Stk2.push (ptop->right);                Stk1.pop (); }            }Else{ while(!stk2.empty ())                    {treenode* ptop = Stk2.top (); Level_vec.push_back (Ptop->val);if(Ptop->right! = NULL)//Stack first right and left, left and right after stack (odd layer)Stk1.push (Ptop->right);if(Ptop->left! = NULL) Stk1.push (ptop->left);                                   Stk2.pop ();        }} result.push_back (Level_vec); }returnResult }};

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

Print binary tree in zigzag order

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.