zoukankan      html  css  js  c++  java
  • 二叉树的递归遍历和非递归遍历

    node 节点定义

        public static class Node{
            public int val;
            public Node left;
            public Node right;
            
            public Node(int val){
                this.val = val;
            }
        }
        

    递归前序遍历:

    public static void preOrder(Node head){
            if (head != null ) {
                System.out.print(head.val);
                preOrder(head.left);
                preOrder(head.right);
            }
        }

    非递归前序遍历:先遍历当前节点,再遍历他的左子树,再到右子树。每个节点都保存着左右子树的信息。

    因为当前节点被弹出,所以必须要先保存他的右子树。如果不将右子树不压栈的话,将会丢失信息。

    public static void preOrder01(Node head) {
            
            if (head == null) {
                return;
            }
            Stack<Node> stack = new Stack<>();
            stack.push(head);
            
            while(!stack.isEmpty()){
                
                Node cur = stack.pop();
                System.out.println(cur.val);
                
                if( cur.right != null ){
                    stack.push(cur.right);
                }
                if (cur.left != null ) {
                    stack.push(cur.left);
                }
            }
            
        }

    中序递归遍历:

    public static void midOrder(Node head){
            if (head != null) {
                preOrder(head.left);
                System.out.print(head.val);
                preOrder(head.right);
            }
        }

    中序非递归遍历: 一直将他的左子树压栈。 一直到左子树最左的节点。  

    public static void midOder01(Node head){
            if (head == null){
                return ;
            }
            
            Stack<Node> stack = new Stack<>();
            stack.push(head);
            
            while(!stack.empty() || head != null){
                if( head.left != null ){
                    stack.push(head.left);
                }else {
                    head = stack.pop();
                    System.out.println(head.val);
                    if (head.right != null) {
                        stack.push(head.right);
                    }
                }
            }
        }

    后序递归遍历:

    public static void laterOrder(Node head){
            if (head != null) {
                laterOrder(head.left);
                laterOrder(head.right);
                System.out.println(head.val);
            }
        }

    后序非递归遍历:

    维护两个栈,第一个栈遍历树的顺序是 中右左

    第二个  左右中。

    public static void laterOrder1(Node head) {
            
            if (head == null) {
                return ;
            }
            Stack<Node> s1 = new Stack<>();
            Stack<Node> s2 = new Stack<>();
            
            s1.push(head);
            while(!s1.empty()){
                
                head = s1.pop();
                s2.push(head);
                if (head.right != null) {
                    s1.push(head.left);
                }
                if (head.left != null) {
                    s1.push(head.right);
                }
                
            }
            while(!s2.empty()){
                head = s2.pop();
                System.out.println(head.val);
            }
        }
        
  • 相关阅读:
    Mysql元数据分析
    python编码encode和decode
    自己写的Python数据库连接类和sql语句拼接方法
    【甘道夫】Sqoop1.99.3基础操作--导入Oracle的数据到HDFS
    SVN配置以及自己主动部署到apache虚拟文件夹
    css中使用id和class 的不同
    Android OpenGL ES(七)----理解纹理与纹理过滤
    一键安装 redmine on windows 和发邮件设置
    足球大数据:致足球怀疑论者-The Counter(s)-Reformation反教条改革
    【Android进阶篇】Fragment的两种载入方式
  • 原文地址:https://www.cnblogs.com/lijins/p/10163304.html
Copyright © 2011-2022 走看看