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