二叉樹的先中後遍歷

二叉樹示意圖數據結構

 

節點的數據結構:this

    public static class Node {
        public int value;
        public Node left;
        public Node right;

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

 

遞歸版:

先來看這一段代碼:spa

public static void Recur(Node head) {
        if (head == null) {
            return;
        }
        preOrderRecur(head.left);
        preOrderRecur(head.right);
}

 

這一段代碼執行過程當中經歷節點的狀況以下圖:code

因而可知,由於系統棧的緣由,每個節點都訪問了3次。blog

先序遍歷的順序是中左右,中序遍歷的順序使左中右,後序遍歷的順序是左右中。遞歸

其中先序遍歷就是每一個節點第一次出現的時候,答案就是1,2,4,5,3,6,7class

其中中序遍歷就是每一個節點第二次出現的時候,答案就是4,2,5,1,6,3,7二叉樹

其中後序序遍歷就是每一個節點第三次出現的時候,答案就是4,5,2,6,7,3,1遍歷

 

代碼以下:im

 

先序遍歷

public static void preOrderRecur(Node head) {
        if (head == null) {
            return;
        }
        System.out.print(head.value + " ");
        preOrderRecur(head.left);
        preOrderRecur(head.right);
    }

 

中序遍歷

public static void inOrderRecur(Node head) {
        if (head == null) {
            return;
        }
        inOrderRecur(head.left);
        System.out.print(head.value + " ");
        inOrderRecur(head.right);
    }

 

後序遍歷

public static void posOrderRecur(Node head) {
        if (head == null) {
            return;
        }
        posOrderRecur(head.left);
        posOrderRecur(head.right);
        System.out.print(head.value + " ");
    }

 

非遞歸版:

 

先序遍歷

使用一個輔助棧,用於壓入節點,壓入的規則是:

1.若是當前節點不存在,則不進行任何操做。

2.先壓入二叉樹的右節點,在壓入二叉樹的左節點,這樣彈出的時候就會先彈出左節點,在彈出右節點。

public static void preOrderUnRecur(Node head) {
        if (head != null) {
            Stack<Node> stack = new Stack<Node>();
            stack.add(head);
            while (!stack.isEmpty()) {
                head = stack.pop();
                System.out.print(head.value + " ");
                if (head.right != null) {
                    stack.push(head.right);
                }
                if (head.left != null) {
                    stack.push(head.left);
                }
            }
        }
}

 

中序遍歷

使用一個輔助棧,用於壓入節點,壓入的規則是:

1.若是當前節點不爲空,則壓入棧,當前節點指向左節點。

2.若是當前節點爲空,則從棧中彈出節點並打印,彈出節點變爲當前節點,當前節點指向的右節點。

public static void inOrderUnRecur(Node head) {
        if (head != null) {
            Stack<Node> stack = new Stack<Node>();
            while (!stack.isEmpty() || head != null) {
                if (head != null) {
                    stack.push(head);
                    head = head.left;
                } else {
                    head = stack.pop();
                    System.out.print(head.value + " ");
                    head = head.right;
                }
            }
        }

}

 

後序遍歷

後序遍歷的順序是左右中,先序遍歷的順序是中左右,若是咱們把先序遍歷的順序改成中右左,中右左反過來就是左右中,也就是後序遍歷。

使用一個輔助棧,用於壓入節點,壓入的規則是:

1.若是當前節點爲空,不進行任何操做。

2.先壓入二叉樹的左節點,在壓入二叉樹的右節點。

public static void posOrderUnRecur1(Node head) {
        if (head != null) {
            Stack<Node> s1 = new Stack<Node>();
            Stack<Node> s2 = new Stack<Node>();
            s1.push(head);
            while (!s1.isEmpty()) {
                head = s1.pop();
                s2.push(head);
                if (head.left != null) {
                    s1.push(head.left);
                }
                if (head.right != null) {
                    s1.push(head.right);
                }
            }
            while (!s2.isEmpty()) {
                System.out.print(s2.pop().value + " ");
            }
        }
    }
相關文章
相關標籤/搜索