二叉树的前序 中序 后序 遍历(递归/非递归)

Node 节点

package main;

public class Node {
    int val;
    Node left, right;

    public Node(int val) {
        this.val = val;
    }
}

测试类

package main;

import javafx.util.Pair;

import java.util.Stack;

public class TreeOrder {
    static Node first = new Node(5);

    static void init() {
        first.left = new Node(3);
        first.right = new Node(7);
        first.left.left = new Node(2);
        first.left.right = new Node(4);
        first.right.left = new Node(6);
        first.right.right = new Node(8);
    }

    static void visit(Node root) {
        System.out.print(root.val + " ");
    }

    static void preOrder1(Node root) {
        if (root != null) {
            visit(root);
            preOrder1(root.left);
            preOrder1(root.right);
        }
    }

    static void preOrder2(Node root) {
        Stack<Node> st = new Stack<>();
        while (root != null || !st.empty()) {
            if (root != null) {
                st.push(root);
                visit(root);
                root = root.left;
            } else {
                Node top = st.pop();
                root = top.right;
            }
        }
    }

    static void inOrder1(Node root) {
        if (root != null) {
            inOrder1(root.left);
            visit(root);
            inOrder1(root.right);
        }
    }

    private static void inOrder2(Node root) {
        Stack<Node> st = new Stack<>();
        while (root != null || !st.empty()) {
            if (root != null) {
                st.push(root);
                root = root.left;
            } else {
                Node top = st.pop();
                visit(top);
                root = top.right;
            }
        }
    }

    static void postOrder1(Node root) {
        if (root != null) {
            postOrder1(root.left);
            postOrder1(root.right);
            visit(root);
        }
    }

    private static void postOrder2(Node root) {
        Stack<Pair<Node, Integer>> st = new Stack<>();
        while (root != null || !st.empty()) {
            if (root != null) {
                st.push(new Pair(root, 1));
                root = root.left;
            } else {
                Pair<Node, Integer> p = st.pop();
                Node key = p.getKey();
                Integer value = p.getValue();
                if(value == 1) {
                    st.push(new Pair<>(key, 2));
                    root = key.right;
                } else
                    visit(key);
            }
        }
    }


    public static void main(String[] args) {
        init();
        System.out.println("preOrder");
        preOrder1(first);
        System.out.println();
        preOrder2(first);
        System.out.println();


        System.out.println("inOrder");
        inOrder1(first);
        System.out.println();
        inOrder2(first);

        System.out.println();

        System.out.println("postOrder");
        postOrder1(first);
        System.out.println();
        postOrder2(first);
        System.out.println();
    }
}

参考文章

二叉树前序、中序、后序遍历的非递归写法

posted @ 2019-03-02 14:28  Draymonder  阅读(203)  评论(0编辑  收藏  举报