树的遍历

递归版本:

/*
public class TreeNode {
    int val = 0;
    TreeNode left = null;
    TreeNode right = null;
    public TreeNode(int val) {
        this.val = val;
    }
}*/
public class TreeToSequence {
    public int[][] convert(TreeNode root) {
        ArrayList<Integer> x=new ArrayList<Integer>();
        ArrayList<Integer> z=new ArrayList<Integer>();
        ArrayList<Integer> h=new ArrayList<Integer>();
        xian(x,root);
        zhong(z,root);
        hou(h,root);
        int[][] res=new int[3][x.size()];
        for(int i=0;i<x.size();i++){
            res[0][i]=x.get(i);
            res[1][i]=z.get(i);
            res[2][i]=h.get(i);
        }
        return res;
          
    }
    static void xian(ArrayList list,TreeNode root){
        if(root==null){
           return;
        }
        list.add(root.val);
        xian(list,root.left);
        xian(list,root.right);
    
    }
     static void zhong(ArrayList list,TreeNode root){
         if(root==null){
            return;
        }
        zhong(list,root.left);
        list.add(root.val);
           zhong(list,root.right);
     }
    static void hou(ArrayList list,TreeNode root){
        if(root==null){
          return;
        }
        hou(list,root.left);
          hou(list,root.right);
        list.add(root.val);
        
    }
} 

非递归/*public class TreeNode {    int val = 0;    TreeNode left = null;

    TreeNode right = null;
    public TreeNode(int val) {
        this.val = val;
    }
}*/
public class TreeToSequence {
    public int[][] convert(TreeNode root) {
         if(root == null){
            return null;
        }
        ArrayList<Integer> x = new ArrayList<Integer>();
        ArrayList<Integer> z = new ArrayList<Integer>();
        ArrayList<Integer> h = new ArrayList<Integer>();
        xian(x,root);
        zhong(z,root);
        hou(h,root);
        int[][] res=new int[3][x.size()];
         for(int i = 0; i < x.size(); i++){
            res[0][i] = x.get(i);
            res[1][i] = z.get(i);
            res[2][i] = h.get(i);
        }
        return res;
    }
   void xian(ArrayList<Integer> list,TreeNode root){
       if(root==null) return ;
       Stack<TreeNode> s=new Stack<TreeNode>();
       s.push(root);
       while(!s.isEmpty()){
           TreeNode cur=s.pop();
           list.add(cur.val);
           if(cur.right!=null)    //后进先出,所以先添加右孩子,再添加左孩子
               s.push(cur.right);
           if(cur.left!=null)
               s.push(cur.left);
       }
   }
    
//中序其实就是最简单的回溯
void zhong(ArrayList<Integer> list,TreeNode root){ if(root==null) return ; Stack<TreeNode> s=new Stack<TreeNode>(); TreeNode cur=root; while(!s.isEmpty()||cur!=null){ if(cur!=null){ s.push(cur); cur=cur.left; }else{ cur=s.pop(); list.add(cur.val); cur=cur.right; } } } void hou(ArrayList<Integer> list,TreeNode root){ if(root==null) return ; Stack<TreeNode> s=new Stack<TreeNode>();
    
s.push(root); while(!s.isEmpty()){ TreeNode cur=s.pop(); if(cur.left!=null){ s.push(cur.left); } if(cur.right!=null){ s.push(cur.right); } list.add(cur); } Collections.reverse(list)l } }
原文地址:https://www.cnblogs.com/team42/p/7003333.html