二叉树遍历

Posted xiangtingsulinlin

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了二叉树遍历相关的知识,希望对你有一定的参考价值。

三种遍历方式

1.先序:根左右

2.后序:左右根

3.中序:左根右

先定义一个二叉树类

 class TreeNode {
        int value;
        TreeNode left;
        TreeNode right;

public TreeNode(){}
public TreeNode(int value, TreeNode left, TreeNode right) { this.value = value; this.left = left; this.right = right; } public TreeNode(int value) { this.value = value; } public int getValue() { return value; } public void setValue(int value) { this.value = value; } public TreeNode getLeft() { return left; } public void setLeft(TreeNode left) { this.left = left; } public TreeNode getRight() { return right; } public void setRight(TreeNode right) { this.right = right; } }

递归遍历

//先序递归遍历
    private static void recursionPreOrder(TreeNode root) {
        if (root==null){
            return;
        }
        System.out.print(root.value);
        recursionPreOrder(root.left);
        recursionPreOrder(root.right);
    }

    //后序递归遍历
    private static void recursionBackOrder(TreeNode root) {
        if (root==null){
            return;
        }
        recursionBackOrder(root.left);
        recursionBackOrder(root.right);
        System.out.print(root.value);
    }

    //中序递归遍历
    private static void recursionMiddleOrder(TreeNode root) {
        if (root==null){
            return;
        }
        recursionMiddleOrder(root.left);
        System.out.print(root.value);
        recursionMiddleOrder(root.right);
    }

 

非递归式遍历

因为递归方法是将栈的管理交给了系统,非递归式则需要我们自己去管理,所以我们使用栈去实现它

//非递归式
    //先序
    private static void preOrder(TreeNode root) {
        Stack<TreeNode> nodeStack = new Stack<>();
        while (root != null || !nodeStack.isEmpty()) {
            while (root != null) {
                System.out.println(root.value);
                nodeStack.push(root);
                root = root.left;
            }
            if (!nodeStack.isEmpty()) {
                root = nodeStack.pop().right;
            }
        }
    }

    //中序
    private static void middleOrder(TreeNode root) {
        Stack<TreeNode> nodeStack = new Stack<>();
        while (root != null || !nodeStack.isEmpty()) {
            while (root != null) {
                nodeStack.push(root);
                root = root.left;
            }

            if (!nodeStack.isEmpty()) {
                root = nodeStack.pop();
                System.out.println(root.value);
                root = root.right;
            }
        }
    }

    //后序
    private static void backOrder(TreeNode root) {
        Stack<TreeNode> nodeStack = new Stack<>();
        TreeNode lastNode = root;
        while (root != null || !nodeStack.isEmpty()) {
            while (root != null) {
                nodeStack.push(root);
                root = root.left;
            }
            root = nodeStack.peek();
            if (root.right == null || root.right == lastNode) {
                System.out.println(root.value);
                nodeStack.pop();
                lastNode = root;
                root = null;
            } else {
                root = root.right;
            }
        }
    }

测试代码

 class TreeNode {
        int value;
        TreeNode left;
        TreeNode right;

        public TreeNode() {
        }

        public TreeNode(int value, TreeNode left, TreeNode right) {
            this.value = value;
            this.left = left;
            this.right = right;
        }

        public TreeNode(int value) {
            this.value = value;
        }

        public int getValue() {
            return value;
        }

        public void setValue(int value) {
            this.value = value;
        }

        public TreeNode getLeft() {
            return left;
        }

        public void setLeft(TreeNode left) {
            this.left = left;
        }

        public TreeNode getRight() {
            return right;
        }

        public void setRight(TreeNode right) {
            this.right = right;
        }
    }
public static void main(String args[]) {
        //二叉树遍历
        TreeNode node1 = new TreeNode(7);
        TreeNode node2 = new TreeNode(8);
        TreeNode node3 = new TreeNode(6, node1, node2);

        TreeNode node4 = new TreeNode(4);
        node4.right = node3;

        TreeNode node5 = new TreeNode(2);
        node5.left = node4;

        TreeNode root = new TreeNode(1);
        root.left = node5;

        TreeNode node6 = new TreeNode(5);

        TreeNode node7 = new TreeNode(3);

        node7.right = node6;

        root.right = node7;

//        System.out.println("先序");
//        recursionPreOrder(root);
//        System.out.println();
//        System.out.println("后序");
//        recursionBackOrder(root);
//        System.out.println();
//        System.out.println("中序");
//        recursionMiddleOrder(root);

//        preOrder(root);
        backOrder(root);
//        middleOrder(root);
//        postorderTraversal(root);
}

 



以上是关于二叉树遍历的主要内容,如果未能解决你的问题,请参考以下文章

编程实现以上二叉树中序遍历操作,输出遍历序列,求写代码~~

c++二叉树按层序遍历顺序输入(由上到下),先序遍历顺序输出,求完整代码

代码随想录--二叉树

二叉树的非递归遍历怎么写?

二叉树遍历和延伸

二叉树的遍历