二叉树的遍历

二叉树图解

二叉树的前序遍历

前序遍历:先输出父节点,再遍历左子树和右子树

   //前序遍历  根左右
    public void preSearch() {
        System.out.println(this);
        if (this.left != null) {
            this.left.preSearch();
        }
        if (this.right != null) {
            this.right.preSearch();
        }
    }

二叉树的中序遍历

中序遍历: 先遍历左子树,再输出父节点,再遍历右子树

 //中序遍历 左根右
    public void midSearch() {
        if (this.left != null) {
            this.left.midSearch();
        }
        System.out.println(this);
        if (this.right != null) {
            this.right.midSearch();
        }
    }

二叉树后续遍历

后续遍历:先遍历左子树,再遍历右子树,最后输出父节点

    //后序遍历 左右根
    public void backSearch() {
        if (this.left != null) {
            this.left.backSearch();
        }
        if (this.right != null) {
            this.right.backSearch();
        }
        System.out.println(this);
    }
原文地址:https://www.cnblogs.com/liuzhidao/p/13885207.html