【编程题】二叉树的前中后序遍历,循环和递归(Java实现)

标题【编程题】二叉树的前中后序遍历,循环和递归(Java实现)

package niuke;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Stack;
public class TraversalTree {
    public static void main(String[] args){
        /*
           定义树结构为
                    0
              1             2
          3       4     5        6
         */
        TreeNode root=new TreeNode(0);
        root.left=new TreeNode(1);
        root.right=new TreeNode(2);
        root.left.left=new TreeNode(3);
        root.left.right=new TreeNode(4);
        root.right.left=new TreeNode(5);
        root.right.right=new TreeNode(6);
        ArrayList<Integer>preList=preTree(root);//和中序类似的方法循环的前序
        System.out.println(Arrays.toString(preList.toArray()));
        ArrayList<Integer>preList2=preTree2(root);//循环的前序
        System.out.println(Arrays.toString(preList2.toArray()));
        ArrayList<Integer>preList1=preTree1(root);//和后序类似的方法写的循环前序
        System.out.println(Arrays.toString(preList1.toArray()));
        ArrayList<Integer>preOrderList=preOrderTree(root,new ArrayList<>());//递归前序
        System.out.println(Arrays.toString(preOrderList.toArray()));
        ArrayList<Integer>inList=inTree(root);//循环的中序
        System.out.println(Arrays.toString(inList.toArray()));
        ArrayList<Integer>inOrderList=inOrderTree(root,new ArrayList<>());//递归中序
        System.out.println(Arrays.toString(inOrderList.toArray()));
        ArrayList<Integer>afterList=afterTree(root);//循环的后序
        System.out.println(Arrays.toString(afterList.toArray()));
        ArrayList<Integer>afterList2=afterTree2(root);//循环的后序
        System.out.println(Arrays.toString(afterList2.toArray()));
        ArrayList<Integer>afterOrderList=afterOrderTree(root,new ArrayList<>());//递归后序
        System.out.println(Arrays.toString(afterOrderList.toArray()));

    }

    /**和中序类似的方法循环的前序,根左右
     * @param root
     * @return
     */
   public static ArrayList<Integer> preTree(TreeNode root){
       ArrayList<Integer>list=new ArrayList<>();
       if(root==null)
           return list;
       Stack<TreeNode>stack=new Stack<>();
       while (!stack.empty()||root!=null){
           while (root!=null){
               stack.push(root);
               list.add(root.val);
               root=root.left;
           }
           if(!stack.empty()){
               root=stack.pop();
               root=root.right;
           }
       }
       return list;
    }
    /*循环的前序,根左右
     * @param root
     * @return
     */
    public static ArrayList<Integer> preTree2(TreeNode root){
        ArrayList<Integer>list=new ArrayList<>();
        if(root==null)
            return list;
        Stack<TreeNode>stack=new Stack<>();
        stack.push(root);
        while (!stack.empty()){
           root=stack.pop();
           list.add(root.val);
           if(root.right!=null)
               stack.push(root.right);
           if(root.left!=null)
               stack.push(root.left);
        }
        return list;
    }
    /**中序遍历,左根右
     * @param root
     * @return
     */
    public static ArrayList<Integer> inTree(TreeNode root) {
        ArrayList<Integer> list = new ArrayList<>();
        if (root == null)
            return list;
        Stack<TreeNode>stack=new Stack<>();
        while (!stack.empty()||root!=null){
            while (root!=null){
                stack.push(root);
                root=root.left;
            }
            if(!stack.empty()){
                root=stack.pop();
                list.add(root.val);
                root=root.right;
            }
        }
        return list;
    }

    /**循环的后序遍历,左右根
     * @param root
     * @return
     */
    public static ArrayList<Integer> afterTree(TreeNode root) {
        ArrayList<Integer> list = new ArrayList<>();
        if (root == null)
            return list;
        Stack<TreeNode>stack=new Stack<>();
        stack.push(root);
        while (!stack.isEmpty()){
            TreeNode node=stack.pop();
            if(node.left!=null)
               stack.push(node.left);
            if(node.right!=null){
                stack.push(node.right);
            }
            list.add(0,node.val);
        }
        return list;
   }
    /*循环的后序,左右根
         * @param root
         * @return
         */
    public static ArrayList<Integer> afterTree2(TreeNode root){
        ArrayList<Integer>list=new ArrayList<>();
        if(root==null)
            return list;
        Stack<TreeNode>stack1=new Stack<>();
        Stack<TreeNode>stack2=new Stack<>();
        stack1.push(root);
        while (!stack1.empty()){
            root=stack1.pop();
            stack2.push(root);
            if(root.left!=null)
                stack1.push(root.left);
            if(root.right!=null)
                stack1.push(root.right);

        }
        while (!stack2.empty())
            list.add(stack2.pop().val);
        return list;
    }
    /**和后序类似的方法写的循环前序,根左右
     * @param root
     * @return
     */
    public static ArrayList<Integer> preTree1(TreeNode root) {
        ArrayList<Integer> list = new ArrayList<>();
        if (root == null)
            return list;
        Stack<TreeNode>stack=new Stack<>();
        stack.push(root);
        while (!stack.isEmpty()){
            TreeNode node=stack.pop();
            list.add(node.val);
            if(node.right!=null){
                stack.push(node.right);
            }
            if(node.left!=null)
                stack.push(node.left);
        }
        return list;
    }

    /**递归前序遍历,根左右
     * @param root
     * @param list
     * @return
     */
    public static ArrayList<Integer> preOrderTree(TreeNode root,ArrayList<Integer>list){
        if(root==null)
            return list;
        list.add(root.val);
        preOrderTree(root.left,list);
        preOrderTree(root.right,list);
        return list;
    }
    /**递归中序遍历,左根右
     * @param root
     * @param list
     * @return
     */
    public static ArrayList<Integer> inOrderTree(TreeNode root,ArrayList<Integer>list){
        if(root==null)
            return list;
        inOrderTree(root.left,list);
        list.add(root.val);
        inOrderTree(root.right,list);
        return list;
    }
    /**递归后序遍历,左右根
     * @param root
     * @param list
     * @return
     */
    public static ArrayList<Integer> afterOrderTree(TreeNode root,ArrayList<Integer>list){
        if(root==null)
            return list;
        afterOrderTree(root.left,list);
        afterOrderTree(root.right,list);
        list.add(root.val);
        return list;
    }

}

/**
 * 定义树
 */
class TreeNode{
    int val;
    TreeNode left;
    TreeNode right;
    TreeNode(int val){
        this.val=val;
    }
    TreeNode(){}
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值