Print binary tree in the order of glyphs-Sword point offer

Source: Internet
Author: User

Print binary tree title in zigzag order description

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.

Ideas
    1. According to test instructions, the order of the nodes in each row is the opposite, we can use two stacks to store each other, and one stack accesses the top element of the stack according to the "left node-right node" order, while the other stack accesses the top element of the stack according to the order of the "right subtree, left dial hand tree". Until two stacks are empty
Code
ImportJava.util.ArrayList;ImportJava.util.Stack;/*public classTreeNode{intval =0;TreeNodeleft = null;TreeNoderight = NULL; PublicTreeNode(intval) {this.val = val; }}*/public classSolution{PublicArrayList<ArrayList<Integer> >Print(TreeNodeProot) {ArrayList<ArrayList<Integer>>result= newArrayList<ArrayList<Integer>> ();if(Proot = = null) {return result; }Stack<TreeNode> stack1 = newStack<TreeNode> ();Stack<TreeNode> Stack2 = newStack<TreeNode> ();ArrayList<Integer> list = newArrayList<Integer> (); List.add (Proot.val);result. Add (list); Stack1.push (Proot); while(Stack1.isempty () | | stack2.isempty ()) {if(Stack1.isempty () && stack2.isempty ()) { Break; }ArrayList<Integer> temp = newArrayList<Integer> ();if(Stack2.isempty ()) { while(!stack1.isempty ()) {if(Stack1.peek (). Right! = null)                        {Temp.add (Stack1.peek (). Right.val);                    Stack2.push (Stack1.peek (). right); }if(Stack1.peek (). Left! = null)                        {Temp.add (Stack1.peek (). Left.val);                    Stack2.push (Stack1.peek (). left);                } stack1.pop (); }            }Else{ while(!stack2.isempty ()) {if(Stack2.peek (). Left! = null)                        {Temp.add (Stack2.peek (). Left.val);                    Stack1.push (Stack2.peek (). left); }if(Stack2.peek (). Right! = null)                        {Temp.add (Stack2.peek (). Right.val);                    Stack1.push (Stack2.peek (). right);                } stack2.pop (); }            }if(Temp.size () >0) {result. Add (temp); }        }return result; }}
Print binary tree title in zigzag order description

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.

Ideas
    1. According to test instructions, the order of the nodes in each row is the opposite, we can use two stacks to store each other, and one stack accesses the top element of the stack according to the "left node-right node" order, while the other stack accesses the top element of the stack according to the order of the "right subtree, left dial hand tree". Until two stacks are empty
Code
ImportJava.util.ArrayList;ImportJava.util.Stack;/*public classTreeNode{intval =0;TreeNodeleft = null;TreeNoderight = NULL; PublicTreeNode(intval) {this.val = val; }}*/public classSolution{PublicArrayList<ArrayList<Integer> >Print(TreeNodeProot) {ArrayList<ArrayList<Integer>>result= newArrayList<ArrayList<Integer>> ();if(Proot = = null) {return result; }Stack<TreeNode> stack1 = newStack<TreeNode> ();Stack<TreeNode> Stack2 = newStack<TreeNode> ();ArrayList<Integer> list = newArrayList<Integer> (); List.add (Proot.val);result. Add (list); Stack1.push (Proot); while(Stack1.isempty () | | stack2.isempty ()) {if(Stack1.isempty () && stack2.isempty ()) { Break; }ArrayList<Integer> temp = newArrayList<Integer> ();if(Stack2.isempty ()) { while(!stack1.isempty ()) {if(Stack1.peek (). Right! = null)                        {Temp.add (Stack1.peek (). Right.val);                    Stack2.push (Stack1.peek (). right); }if(Stack1.peek (). Left! = null)                        {Temp.add (Stack1.peek (). Left.val);                    Stack2.push (Stack1.peek (). left);                } stack1.pop (); }            }Else{ while(!stack2.isempty ()) {if(Stack2.peek (). Left! = null)                        {Temp.add (Stack2.peek (). Left.val);                    Stack1.push (Stack2.peek (). left); }if(Stack2.peek (). Right! = null)                        {Temp.add (Stack2.peek (). Right.val);                    Stack1.push (Stack2.peek (). right);                } stack2.pop (); }            }if(Temp.size () >0) {result. Add (temp); }        }return result; }}

Print binary tree in the order of glyphs-Sword point offer

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.