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);
            }
        }
        
  • 相关阅读:
    判断php变量是否定义,是否为空
    HTTP Client 编写
    推荐《冒号课堂——编程范式与OOP思想》
    一些免费的HTML编辑器
    如何判断mysql中数据表中两个列之间的相同记录和不同记录
    PostgreSQL 8.4, SQL Server 2008, MySQL 5.1比较
    JDBC纵览
    使用jdbc连接sql数据库
    关于PHP中变量的判定
    如何判断数据库中是否存在一个数据表
  • 原文地址:https://www.cnblogs.com/lijins/p/10163304.html
Copyright © 2011-2022 走看看