Sword refers to Offer-56-Print the binary tree in zigzag order

Title description

Please implement a function to print the binary tree in a zigzag pattern, 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 the other lines are printed from left to right. And so on.

Idea analysis

  1. Each two layers has its own printing order, so you need to consider how to print the first layer, and then print the second layer from another direction. And the specific node sequence needs to be passed to the next layer. Therefore, two stacks are considered here for storage. The first stack stack1 stores the printing order of the first layer, which can be recorded as the odd layer printing order. The second stack Stack2 stores the printing order of the second layer, which can be recorded as the printing order of even-numbered layers. After each layer is traversed, the traversed nodes are popped out.

Code

import java.util.ArrayList;
import java.util.Stack;
/*
public class TreeNode {
    int val = 0;
    TreeNode left = null;
    TreeNode right = null;

    public TreeNode(int val) {
        this.val = val;

    }

}
*/
public class Solution {
    
    
    public ArrayList<ArrayList<Integer> > Print(TreeNode pRoot) {
    
    
     
     ArrayList<ArrayList<Integer>> result = new ArrayList<>();
     if(pRoot == null) return result;
        ArrayList<Integer> singlePath = new ArrayList<>();
     Stack<TreeNode> stack1 = new Stack<>();//用于存储奇数层的节点
     Stack<TreeNode> stack2 = new Stack();//用于存储偶偶数节点
     stack1.push(pRoot);
     while(!stack1.isEmpty()||!stack2.isEmpty()){
    
    
        while(!stack1.isEmpty()){
    
    
            TreeNode tempNode = stack1.peek();
            singlePath.add(tempNode.val);
            //将它的子树结点保存到stack2,从左往右
            if(tempNode.left!=null){
    
    
                stack2.push(tempNode.left);
            }
            if(tempNode.right!=null){
    
    
                stack2.push(tempNode.right);
            }
            //将stack1中的节点抛出
            stack1.pop();
        }
         //先将一层的数据存入result中
         if(!singlePath.isEmpty()){
    
    
             result.add(singlePath);
             singlePath = new ArrayList<>();
         }
         //开始偶数行的数据
         while(!stack2.isEmpty()){
    
    
             TreeNode tempNode = stack2.peek();
             singlePath.add(tempNode.val);
             //将第二层的下一层子树结点加入到stack1中(奇数层加入进去)
             if(tempNode.right!=null){
    
    
                 stack1.push(tempNode.right);
             }
             if(tempNode.left!=null){
    
    
                 stack1.push(tempNode.left);
             }
             stack2.pop();
         }
         if(!singlePath.isEmpty()){
    
    
             result.add(singlePath);
             singlePath = new ArrayList<>();
         }
     }
        return result;
    }

}

Guess you like

Origin blog.csdn.net/H1517043456/article/details/107590676