二叉樹遍歷總結(先序||中序||後序||按層遍歷||之字遍歷&&遞歸||非遞歸)

在這裏插入圖片描述
先序遍歷:8 6 5 7 10 9 11
後序遍歷:5 7 6 9 11 10 8
中序遍歷:5 6 7 8 9 10 11
按層遍歷:8 6 10 5 7 9 11
之字遍歷:8 10 6 5 7 9 11

先序遍歷

遞歸
    public static void printBTPerRecursion(TreeNode root){
        if (root!=null){
            System.out.print(root.value+" ");
            printBTPerRecursion(root.left);
            printBTPerRecursion(root.right);
        }
    }
非遞歸
 public static void printBTPerUnrecursion(TreeNode node){
        while (node!=null||!stack.isEmpty()){
            if (node!=null){
                System.out.print(node.value+" ");
                stack.push(node);
                node = node.left;
            }
            else {
                node = stack.pop();
                node = node.right;
            }
        }
    }

中序遍歷

遞歸
public static void printBTMidRecursion(TreeNode root){
        if (root!=null){
            printBTMidRecursion(root.left);
            System.out.print(root.value+" ");
            printBTMidRecursion(root.right);
        }
    }
非遞歸
 public static void printBTMidUnrecursion(TreeNode node){
        while (node!=null||!stack.isEmpty()){
            if (node!=null){
                stack.push(node);
                node = node.left;
            }else {
                TreeNode node1 = stack.pop();
                System.out.print(node1.value+" ");
                node = node1.right;
            }
        }
    }

後序遍歷

遞歸
    public static void printBTBackRecursion(TreeNode root){
        if (root!=null){
            printBTBackRecursion(root.left);
            printBTBackRecursion(root.right);
            System.out.print(root.value+" ");
        }
    }
非遞歸()
//非遞歸後序遍歷 參考自https://blog.csdn.net/zhuqiuhui/article/details/51319165
    public static void printBTBackUnrecursion(TreeNode node){
        if (node==null)return;
        TreeNode curNode;
        TreeNode lastVisitNode;
        curNode = node;
        lastVisitNode = null;
        //先找到最左節點
        while (curNode!=null){
            stack.push(curNode);
            curNode = curNode.left;
        }
        while (!stack.empty()){
            curNode = stack.pop();
            //如果此結點右節點不爲空且沒有被訪問過,那麼將它再次入棧,並順着它的右節點再次入棧左節點
            if (curNode.right!=null&&curNode.right!=lastVisitNode){
                stack.push(curNode);
                curNode = curNode.right;
                while (curNode!=null){
                    stack.push(curNode);
                    curNode = curNode.left;
                }
            }else {
                System.out.print(curNode.value+" ");
                lastVisitNode = curNode;
            }
        }
    }

按層遍歷(使用隊列實現)

遞歸
public static void printLayerRecursion(TreeNode root) throws InterruptedException {
        System.out.print(root.value+" ");
        if (root.left!=null)queue.add(root.left);
        if (root.right!=null)queue.add(root.right);
        TreeNode treeNode = queue.poll();
        if (treeNode!=null)
        printLayerRecursion(treeNode);
    }
非遞歸
 public static void printLayerUnrecursion(TreeNode node){
        if (node==null)return;
        queue.add(node);
        while (!queue.isEmpty()){
            TreeNode node1 = queue.poll();
            System.out.print(node1.value+" ");
            if (node1.left!=null)queue.add(node1.left);
            if (node1.right!= null)queue.add(node1.right);
        }
    }

之字遍歷

非遞歸(需要兩個棧,兩個棧交替裝入每一層的結點,一個棧先裝左節點再裝右節點,另一個棧先裝右節點再裝左節點)
public static void printBTlikeZHI(TreeNode node){
        if (node==null)return;
        stack.push(node);
        int current = 0;
        int next = 1;
        while (!stack.empty()||!otherStack.isEmpty()){
            TreeNode node1 = (TreeNode) stacks[current].pop();
            System.out.print(node1.value+" ");
            if (current==0){
                if (node1.left!=null)stacks[next].push(node1.left);
                if(node1.right!=null)stacks[next].push(node1.right);

            }else {
                if (node1.right!=null)stacks[next].push(node1.right);
                if (node1.left!=null)stacks[next].push(node1.left);

            }
            if (stacks[current].empty()){
                current = 1-current;
                next = 1-next;
            }
        }
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章