【劍指Offer】二叉樹——先序、中序、後序、層序遍歷

package cn.dzp.flyroc.offer;

import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;

public class TreeRecur {

    /*題目描述:分別實現樹的遞歸方式和非遞歸方式的先序、中序、後序遍歷以及層序遍歷*/


    /*先序遍歷:根節點——>左子樹——>右子樹
     * 中序遍歷:左子樹——>根節點——>右子樹
     * 後序遍歷:左子樹——>右子樹——>根節點*/

    //定義TreeNode    public static class TreeNode {

        int val;
        TreeNode left = null;
        TreeNode right = null;

        TreeNode(int val) {
            this.val = val;
        }
    }

    /*
     *遞歸方式
     * */

    //先序遍歷
    public static void preOrderRecur(TreeNode root) {
        if (root == null) {
            return;
        }
        System.out.print(root.val + " ");
        preOrderRecur(root.left);
        preOrderRecur(root.right);
    }


    //中序遍歷
    public static void inOrderRecur(TreeNode root) {
        if (root == null) {
            return;
        }
        inOrderRecur(root.left);
        System.out.print(root.val + " ");
        inOrderRecur(root.right);
    }


    //後序遍歷
    public static void posOrderRecur(TreeNode root) {
        if (root == null) {
            return;
        }
        posOrderRecur(root.left);
        posOrderRecur(root.right);
        System.out.print(root.val + " ");
    }



    /*
     * 非遞歸方式
     * */

    //先序遍歷
    public static void preOrderUnRecur(TreeNode root) {

        if (root != null) {
            Stack<TreeNode> stack = new Stack<>();
            stack.push(root);

            while (!stack.isEmpty()) {

                root = stack.pop();//將根結點壓入棧
                System.out.print(root.val + " ");//打印當前節點的值
                if (root.right != null) {
                    stack.push(root.right);
                }
                if (root.left != null) {
                    stack.push(root.left);
                }
            }
        }
        System.out.println();
    }


    //中序遍歷
    public static void inOrderUnRecur(TreeNode root){

        if(root != null){
            Stack<TreeNode> stack = new Stack<>();

            while(!stack.isEmpty() || root != null){

                if(root != null){
                    stack.push(root);
                    root = root.left;
                }else {
                    root = stack.pop();
                    System.out.print(root.val + " ");
                    root = root.right;
                }
            }
        }
        System.out.println();
    }


    //後序遍歷
    public static void posOrderUnRecur(TreeNode root){

        if(root != null){

            Stack<TreeNode> s1 = new Stack<>();
            Stack<TreeNode> s2 = new Stack<>();
            s1.push(root);

            while(!s1.isEmpty()){

                root = s1.pop();
                s2.push(root);

                if(root.left != null){
                    s1.push(root.left);
                }
                if(root.right != null){
                    s1.push(root.right);
                }
            }
            while(!s2.isEmpty()){
                System.out.print(s2.pop().val + " ");
            }
        }
        System.out.println();
    }



    /*
    * 層序遍歷
    * */

    //使用對列實現
    public static void levelOrder(TreeNode root){

        if(root == null){
            return;
        }

        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);

        while(!queue.isEmpty()){

            root = queue.poll();
            System.out.print(root.val + " ");

            if(root.left != null){

                queue.offer(root.left);
            }
            if(root.right != null){

                queue.offer(root.right);
            }
        }
    }
}
相關文章
相關標籤/搜索