二叉树遍历递归和非递归实现

import java.util.ArrayList;
import java.util.Collections;
import java.util.Stack;

/**
 * 二叉树的前、中、后三种遍历方式递归实现和非递归实现
 */
public class PrintTree {

    //前序遍历递归
    public static ArrayList<Integer> preRecursive(ArrayList<Integer> result,TreeNode root){
        if(root!=null){
            result.add(root.val);
            if(root.left!=null){
                preRecursive(result,root.left);
            }
            if (root.right!=null){
                preRecursive(result,root.right);
            }
        }
        return result;
    }

    //中序遍历递归
    public static ArrayList<Integer> inRecursive(ArrayList<Integer> result,TreeNode root){
        if(root!=null){
            if(root.left!=null){
                inRecursive(result,root.left);
            }
            result.add(root.val);
            if (root.right!=null){
                inRecursive(result,root.right);
            }
        }
        return result;
    }

    //后序遍历递归
    public static ArrayList<Integer> sufRecursive(ArrayList<Integer> result,TreeNode root){
        if(root!=null){
            if(root.left!=null){
                sufRecursive(result,root.left);
            }
            if (root.right!=null){
                sufRecursive(result,root.right);
            }
            result.add(root.val);
        }
        return result;
    }

    /**
     * 前序非递归实现
     * 核心思路:(栈实现)
     * 根节点入栈,top节点出栈,打印出栈节点,出栈节点存在子节点,子节点从左至右依次入栈,
     * 循环上一步直至栈空。
     * @param root
     * @return
     */
    public static ArrayList<Integer> preNotRecursive(TreeNode root){
        ArrayList<Integer> result = new ArrayList<>();
        Stack<TreeNode> stack = new Stack<>();
        if(root!=null){
            stack.push(root);
        }else{
            return null;
        }
        TreeNode tempNode = null;
        while (!stack.isEmpty()){
            tempNode = stack.pop();
            result.add(tempNode.val);
            if(tempNode.right!=null){
                stack.push(tempNode.right);
            }
            if(tempNode.left!=null){
                stack.push(tempNode.left);
            }
        }
        return result;
    }

    /**
     * 中序非递归实现
     * 核心思路:(栈实现)
     *  根节点开始找到所有左孩子,依次入栈
     *  栈顶弹出最后一个左孩子,打印弹出节点
     *  检查弹出节点是否存在右子树,存在压入栈中,
     *  当前节点作为根节点循环以上3步,直到栈空
     * @param root
     * @return
     */
    public static ArrayList<Integer> inNotRecursive(TreeNode root){
        ArrayList<Integer> result = new ArrayList<>();
        Stack<TreeNode> stack = new Stack<>();
        if(root==null){
            return null;
        }
        TreeNode tempNode = null;
        while (root!=null){
            stack.push(root);
            tempNode = root;
            while (tempNode.left!=null){
                stack.push(tempNode.left);
                tempNode = tempNode.left;
            }
            TreeNode top = stack.pop();
            result.add(top.val);
            root = top.right;
            while (root ==null&&!stack.isEmpty()){
                top = stack.pop();
                result.add(top.val);
                root = top.right;
            }
        }
        return result;
    }

    /**
     * 后序非递归实现
     * 核心思路:
     * 前序遍历左右相反,结果逆序
     * @param root
     * @return
     */
    public static ArrayList<Integer> sufNotRecursive(TreeNode root){
        ArrayList<Integer> result = new ArrayList<>();
        Stack<TreeNode> stack = new Stack<>();
        if(root!=null){
            stack.push(root);
        }else{
            return null;
        }
        TreeNode tempNode = null;
        while (!stack.isEmpty()){
            tempNode = stack.pop();
            result.add(tempNode.val);
            if(tempNode.left!=null){
                stack.push(tempNode.left);
            }
            if(tempNode.right!=null){
                stack.push(tempNode.right);
            }
        }
        Collections.reverse(result);
        return result;
    }

    public static void main(String[] args) {
        int [] pre={1,2,4,7,3,5,6,8};
        int [] in = {4,7,2,1,5,3,8,6};
        TreeNode root = ReConstructBinaryTree.reConstructBinaryTree(pre,in);
        ArrayList<Integer> result1 = new ArrayList<>();
        preRecursive(result1,root);
        //[1,2,4,7,3,5,6,8]
        System.out.println(result1.toString());
        result1 =preNotRecursive(root);
        //[1,2,4,7,3,5,6,8]
        System.out.println(result1.toString());

        ArrayList<Integer> result2 = new ArrayList<>();
        inRecursive(result2,root);
        // [4, 7, 2, 1, 5, 3, 8, 6]
        System.out.println(result2.toString());
        result2 = inNotRecursive(root);
        // [4, 7, 2, 1, 5, 3, 8, 6]
        System.out.println(result2.toString());

        ArrayList<Integer> result3 = new ArrayList<>();
        sufRecursive(result3,root);
        //[7, 4, 2, 5, 8, 6, 3, 1]
        System.out.println(result3.toString());
        result3 = sufNotRecursive(root);
        //[7, 4, 2, 5, 8, 6, 3, 1]
        System.out.println(result3.toString());
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值