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





    1. 递归实现


    先序

    public void preOrder(){
        preOrder(root);
    }
    private void preOrder(Node node){
        if(node != null){
            System.out.println(node.value);
            preOrder(node.left);
            preOrder(node.right);
        }
    }
    

    中序

    public void midOrder(){
        midOrder(root);
    }
    private void midOrder(Node node){
        if(node != null){
            midOrder(node.left);
            System.out.println(node.value);
            midOrder(node.right);
        }
    }
    

    后序

    public void postOrder(){
        postOrder(root);
    }
    private void postOrder(Node node){
        if(node != null){
            postOrder(node.left);
            postOrder(node.right);
            System.out.println(node.value);
        }
    }
    




    2. 非递归


    前序

    public void preOrderNew(){
        preOrderNew(root);
    }
    private void preOrderNew(Node node){
        if(node != null){
            LinkedList<Node> list = new LinkedList();
            list.addFirst(node);
    
            while(!list.isEmpty()){
                Node temp = (Node) list.removeFirst();
                if(temp.right != null){
                    list.addFirst(temp.right);
                }
                if(temp.left != null){
                    list.addFirst(temp.left);
                }
                System.out.println(temp.value);
            }
        }
    }
    

    中序

    public void midOrderNew(){
        midOrderNew(root);
    }
    private void midOrderNew(Node node){
        LinkedList<Node> list = new LinkedList();
        while(!list.isEmpty() || node != null){
            if(node != null){
                list.addFirst(node);
                node = node.left;
            }else{
                node = list.removeFirst();
                System.out.println(node.value);
                node = node.right;
            }
        }
    }
    

    后序

    public void postOrderNew(){
        postOrderNew(root);
    }
    private void postOrderNew(Node node){
        if(node != null){
            LinkedList<Node> list1 = new LinkedList();
            LinkedList<Node> list2 = new LinkedList();
            list1.addFirst(node);
    
            while(!list1.isEmpty()){
                Node temp = list1.removeFirst();
                list2.addFirst(temp);
                if(temp.left != null){
                    list1.addFirst(temp.left);
                }
                if(temp.right != null){
                    list1.addFirst(temp.right);
                }
            }
            while(!list2.isEmpty()){
                Node temp = list2.removeFirst();
                System.out.println(temp.value);
            }
        }
    }
    


  • 相关阅读:
    通过远程linux shell传输文件
    Windows下 postgresql安装报错
    python安装tesserocr错误解决办法
    django makemigrations
    Windows下eric安装的几个小问题
    _WinMain结构
    python学习第七天——输入输出、file
    python学习第六天——模块的概念(重要)
    python学习第五天——函数(重点)
    python学习第四天——控制流
  • 原文地址:https://www.cnblogs.com/Howlet/p/12812648.html
Copyright © 2011-2022 走看看