zoukankan      html  css  js  c++  java
  • 二叉树遍历方式

    一、先序遍历

    先序遍历:根------左子树-------右子树

    递归版本

    public static void prePrint(TreeNode root) {
            if (root == null)
                return;
            System.out.print(root.val + " ");
            prePrint(root.left);
            prePrint(root.right);
        }

    非递归版本

    public static void preOrder(TreeNode root) {
            Stack<TreeNode> s = new Stack<>();
            TreeNode p = root;
            while (p != null || !s.empty()) {
                while (p != null) {
                    System.out.print(p.val + " ");
                    s.push(p);
                    p = p.left;
                }
                if (!s.empty()) {
                    p = s.pop();
                    p = p.right;
                }
            }
        }

    二、中序遍历

    中序遍历:左子树-----根-----右子树

    递归版本

    public static void inPrint(TreeNode root) {
            if (root == null)
                return;
            inPrint(root.left);
            System.out.print(root.val + " ");
            inPrint(root.right);
        }

    非递归版本

    public static void inOrder(TreeNode root) {
            Stack<TreeNode> s = new Stack<>();
            TreeNode p = root;
            while (p != null || !s.empty()) {
                while (p != null) {
                    s.push(p);
                    p = p.left;
                }
                if (!s.empty()) {
                    p = s.pop();
                    System.out.print(p.val + " ");
                    p = p.right;
                }
            }
        }

    三、后序遍历

    后序遍历:左子树-----右子树-----根

    递归版本

    public static void postPrint(TreeNode root) {
            if (root == null)
                return;
            postPrint(root.left);
            postPrint(root.right);
            System.out.print(root.val + " ");
        }

    非递归版本

    后序遍历的非递归应该比前面两种遍历的非递归要复杂些,当然这里我们还是需要借助栈来实现。由于后序遍历是左-----右-----根,所以在入栈的时候,应该倒着放,也就是根-----右-----左,这样当出栈的时候才能保证正确的顺序。入栈我们知道了,出栈的时候应该什么时候出呢?两种情况:1、如果当前栈顶左右孩子都为null,2、如果上一个出栈的元素是当前栈顶元素的孩子结点。所以我们需要定义一个变量用来执行上一个出栈的节点。

    public static void postOrder(TreeNode root) {
            Stack<TreeNode> s = new Stack<>();
            s.push(root);
            TreeNode pre = null; //前一个出栈的结点
            TreeNode p = null;
            while(!s.empty()) {
                p = s.peek();
                //如果左右孩子都为空,或者上一个出栈的结点是当前节点的孩子结点的时候才打印当前结点的值,并出栈。
                if(p.left == null && p.right == null || pre != null && (pre == p.left || pre == p.right)) {
                    System.out.print(p.val + " ");
                    pre = s.pop();
                }
                else {
                    if(p.right != null) s.push(p.right);
                    if(p.left != null) s.push(p.left);
                }
            }
        }

    四、层序遍历

    层序遍历:从最上面根那层开始,依次从上往下,从左往右遍历。

    public static void levelPrint(TreeNode root) {
            if (root == null)
                return;
            LinkedList<TreeNode> list = new LinkedList<>();
            list.addLast(root);
            while (!list.isEmpty()) {
                TreeNode node = list.removeFirst();
                System.out.print(node.val + " ");
                if (node.left != null)
                    list.addLast(node.left);
                if (node.right != null)
                    list.addLast(node.right);
            }
        }
  • 相关阅读:
    【Thymeleaf】遇到Current request is not a multipart request不要慌,检查页面中form的属性enctype="multipart/formdata"是否正确就对了
    [CSS]让ul中的li在所属div内成一行居中显示。
    【JS】将yyyyMMdd hh:mm:ss的字符串时间转换为JS时间
    分页资料收集
    【oralce/springboot/jquery】自行实现分页
    【JS】一小时之内显示红饼图标,两小时之内选择黄星图标,否则显示时间
    【JavaScript】给动态生成的链接动态的绑定事件
    VBA在Excel中的应用(四)
    在MOSS 2007中自定义DataView Webpart的数据显示样式
    ASP.NET中的缩略图应用
  • 原文地址:https://www.cnblogs.com/neuzk/p/9485258.html
Copyright © 2011-2022 走看看