zoukankan      html  css  js  c++  java
  • 二叉树的前中后序遍历的递归与非递归算法模版

    1.节点数据结构

    public class Node {
    
        public int value;
        public Node left;
        public Node right;
    
        public Node(int data){
            this.value = value;
        }
    }

    2.递归

    public class Recur {
    
        public void preOrderRecur(Node head){
            if (head == null){
                return;
            }
    
            System.out.println(head.value + " ");
            preOrderRecur(head.left);
            preOrderRecur(head.right);
        }
    
        public void inOrderRecur(Node head){
            if (head == null){
                return;
            }
    
            inOrderRecur(head.left);
            System.out.println(head.value + " ");
            inOrderRecur(head.right);
        }
    
        public void posOrderRecur(Node head){
            if (head == null){
                return;
            }
    
            posOrderRecur(head.left);
            posOrderRecur(head.right);
            System.out.println(head.value + " ");
        }
    }

    3.非递归

    import java.util.Stack;
    
    public class UnRecur {
    
        public void preOrderUnRecur(Node head) {
            System.out.println("pre-order: ");
            if (head != null) {
                Stack<Node> stack = new Stack<>();
                stack.add(head);
                while (!stack.isEmpty()) {
                    head = stack.pop();
                    System.out.println(head.value + " ");
                    if (head.right != null) {
                        stack.push(head.right);
                    }
                    if (head.left != null) {
                        stack.push(head.left);
                    }
                }
            }
        }
    
        public void inOrderUnRecur(Node head) {
            System.out.println("in-order: ");
            if (head != null) {
                Stack<Node> stack = new Stack<>();
                while (!stack.isEmpty() || head != null) {
                    if (head != null) {
                        stack.push(head);
                        head = head.left;
                    } else {
                        head = stack.pop();
                        System.out.println(head.value + " ");
                        head = head.right;
                    }
                }
            }
        }
    
        public void posOrderUnRecur(Node head) {
            System.out.println("pos-order: ");
            if (head != null) {
                Stack<Node> stack = new Stack<>();
                stack.push(head);
                Node c;
                while (!stack.isEmpty()) {
                    c = stack.peek();
                    if (c.left != null && head != c.left && head != c.right) {
                        stack.push(c.left);
                    } else if (c.right != null && head != c.right) {
                        stack.push(c.right);
                    } else {
                        System.out.println(stack.pop().value + " ");
                        head = c;
                    }
                }
    
            }
        }
    }
  • 相关阅读:
    SSAS : 如何在http访问SSAS中指定语言标识
    网络开发:在Socket中发送大文件
    CSS应用及其优先级问题
    SSAS : 使用.NET为SSAS编写自定义程序集
    SSAS : 如何编写自定义挖掘算法
    什么是高清视频
    SSAS : 使用.NET为SSAS编写自定义程序集(二)
    循证架构寻找最适合自己的架构
    有道难题,我的OO解法
    假如我是海洋
  • 原文地址:https://www.cnblogs.com/zhihaospace/p/12751275.html
Copyright © 2011-2022 走看看