• 二叉树的遍历


    二叉树图解

    二叉树的前序遍历

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

       //前序遍历  根左右
        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);
        }
    
  • 相关阅读:
    ASP.NET中Cookie编程的基础知识
    一道编程题
    软件开发一点心得
    迅雷产品经理笔试题
    常用JS 1
    设计模式
    整理思路
    抽象工厂模式 Abstract Factory
    单件模式(Single Pattern)
    序列化
  • 原文地址:https://www.cnblogs.com/liuzhidao/p/13885207.html
Copyright © 2020-2023  润新知