import java.util.*;

/*
 * public class TreeNode {
 *   int val = 0;
 *   TreeNode left = null;
 *   TreeNode right = null;
 * }
 */

public class Solution {
    /**
     * 
     * @param root TreeNode类 the root of binary tree
     * @return int整型二维数组
     */
    public int[][] threeOrders (TreeNode root) {
        // write code here
        int[][] res = new int[3][];
        ArrayList<Integer> list = new ArrayList<>();
        frontSearch(root,list);
        res[0] = list.stream().mapToInt(Integer::intValue).toArray();
        list.clear();
        middleSearch(root,list);
        res[1] = list.stream().mapToInt(Integer::intValue).toArray();
        list.clear();
        lastSearch(root,list);
        res[2] = list.stream().mapToInt(Integer::intValue).toArray();
        return res;
    }
    //非递归前序遍历
    public void frontSearch (TreeNode root,ArrayList<Integer> list) {
        // write code here
        if(root == null){return;}
        Stack<TreeNode> stack = new Stack<>();
        stack.push(root);
        while(!stack.isEmpty()){
            root = stack.pop();
            list.add(root.val);
            if(root.right!=null){
                stack.push(root.right);
            }
            if(root.left!=null){
                stack.push(root.left);
            }
        }
    }
    //非递归中序遍历
    public void middleSearch (TreeNode root,ArrayList<Integer> list) {
        // write code here       
        if(root == null){return;}
        Stack<TreeNode> stack = new Stack<>();
        while(!stack.isEmpty()||root!=null){
            //先压到左子树尽头
            while(root!=null){
                stack.push(root);
                root = root.left;
            }
            if(!stack.isEmpty()){
                root = stack.pop();
                list.add(root.val);
                root = root.right;
            } 
        }
    }
    //非递归后序遍历
    public void lastSearch (TreeNode root,ArrayList<Integer> list) {
        // write code here
        if(root == null){return;}
        Stack<TreeNode> stack = new Stack<>();
        TreeNode pre = null;
        while(!stack.isEmpty()||root!=null){
            //先压到左子树尽头
            while(root!=null){
                stack.push(root);
                root = root.left;
            }
            root = stack.pop();
            if(root.right==null || root.right == pre){
                list.add(root.val);
                pre = root;
                root = null;
            }else{
                stack.push(root);              
                root = root.right;                
            }
        }
    }
    
    //递归前序遍历
    public void frontSearch1 (TreeNode root,ArrayList<Integer> list) {
        // write code here
        if(root == null){return;}
        list.add(root.val);
        if(root.left!=null){
            frontSearch(root.left,list);
        }
        if(root.right!=null){
            frontSearch(root.right,list);
        }      
    }
    //递归中序遍历
    public void middleSearch1 (TreeNode root,ArrayList<Integer> list) {
        // write code here       
        if(root == null){return;}
        if(root.left!=null){
            middleSearch(root.left,list);
        }
        list.add(root.val);
        if(root.right!=null){
            middleSearch(root.right,list);
        } 
    }
    //递归后序遍历
    public void lastSearch1 (TreeNode root,ArrayList<Integer> list) {
        // write code here
        if(root == null){return;}
        if(root.left!=null){
            lastSearch(root.left,list);
        }
        if(root.right!=null){
            lastSearch(root.right,list);
        }
        list.add(root.val);
    }
    
}