Wins the offer-29. Press the zigzag print binary tree (176)

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/qq_38332722/article/details/100542120

29. Press the zigzag print binary tree (176)

  • Description Title: Implement a binary printing function according to a zigzag, i.e., the first row from left to right order of printing, the print order of the second layer is from right to left, left to right in the third row print order, other lines and so on.

  • Ideas: for the odd layer nodes: node requires access right to left. For even layer nodes: node needs to access from left to right.

    Odd-layer nodes from right to left while the access node, and child node (even-numbered layers) is pressed into a blank stack (right child -> left child), in even layers so that the access time is left the right node of the access layer.

    Even layer nodes from left to right while the access node, and child node (odd-numbered layers) is pressed into a blank stack (left child -> right child), so that the odd layers, it is the access right the left node access layer.

  • Code

    package _29.按之字型打印二叉树;
    
    import java.util.ArrayList;
    import java.util.Stack;
    
    public class PrintTreeInSpeical {
        public static ArrayList<ArrayList<Integer> > Print(TreeNode pRoot) {
        	ArrayList<ArrayList<Integer> > print = new ArrayList<ArrayList<Integer> >();
        	if(pRoot == null) return print;
        	
        	//用两个栈实现
        	//stack1:将父结点的孩子按顺序:左->右 进栈
        	Stack<TreeNode> stack1 = new Stack<>();
        	//stack2:将父结点的孩子按顺序:右->左 进栈
        	Stack<TreeNode> stack2 = new Stack<>();
        	
        	TreeNode node = pRoot;
        	stack1.push(node);
        	while(!stack1.isEmpty() || !stack2.isEmpty()){
        		ArrayList<Integer> list = new ArrayList<>();
        		if(!stack1.isEmpty()){
        			while(!stack1.isEmpty()){
            			//获取父结点
            			node = stack1.peek();
            			//将孩子结点入栈到stack2(左->右)
            			if(node.left != null)
            				stack2.push(node.left);
            			if(node.right != null)
            				stack2.push(node.right);
            			
            			//父结点出栈访问
            			node = stack1.pop();
            			list.add(node.val);
            		}
        		}
        		else{
        			while(!stack2.isEmpty()){
            			node = stack2.peek();
            			//将孩子结点入栈stack1(右->左)
            			if(node.right != null)
            				stack1.push(node.right);
            			if(node.left != null)
            				stack1.push(node.left);
            			
            			//父结点出栈访问
            			node = stack2.pop();
            			list.add(node.val);
            		}
        		}
        		print.add(list);
        	}
        	return print;
        }
        
        public static void main(String[] args) {
        	TreeNode root = new TreeNode(1);
    		TreeNode node1 = new TreeNode(2);
    		TreeNode node2 = new TreeNode(3);
    		TreeNode node3 = new TreeNode(4);
    		TreeNode node4 = new TreeNode(5);
    		TreeNode node5 = new TreeNode(6);
    		TreeNode node6 = new TreeNode(7);
    		root.left = node1;
    		root.right = node2;
    		node1.left = node3;
    		node1.right = node4;
    		
    		node2.left = node5;
    		node2.right = node6;
    		ArrayList<ArrayList<Integer>> print = Print(root);
    		System.out.println(print);
    	}
    }
    class TreeNode {
        int val = 0;
        TreeNode left = null;
        TreeNode right = null;
    
        public TreeNode(int val) {
            this.val = val;
        }
    }
    

Guess you like

Origin blog.csdn.net/qq_38332722/article/details/100542120