二叉樹的三種非遞歸遍歷和morris遍歷

1、先序遍歷

public static void printPreOrder(TreeNode node) {
        Queue<TreeNode> queue = new LinkedList<TreeNode>();
        if (node == null)
            return;
        queue.offer(node);
        while (!queue.isEmpty()) {
            TreeNode temp = queue.poll();
            System.out.println(temp.val);
            if (temp.left != null)
                queue.offer(temp.left);
            if (temp.right != null)
                queue.offer(temp.right);
        }
    }

2、中序遍歷

public static void printInOrder(TreeNode node) {
        Stack<TreeNode> stack = new Stack<TreeNode>();
        if (node == null)
            return;
        TreeNode temp = node;
        while (temp != null || !stack.isEmpty()) {
            while (temp != null) {
                stack.add(temp);
                temp = temp.left;
            }
            temp = stack.pop();
            System.out.println(temp.val);
            temp = temp.right;
        }
    }

3、後序遍歷

public static void printPostOrder(TreeNode node) {
        Stack<TreeNode> stack = new Stack<TreeNode>();
        if (node == null)return;
        TreeNode temp = node;
        TreeNode pre = null;
        while (temp != null || !stack.isEmpty()) {
            while (temp != null) {
                stack.add(temp);
                temp = temp.left;
            }
            temp = stack.pop();
            while (temp!=null&&(temp.right == null || pre == temp.right)) {
                pre = temp;
                System.out.println(temp.val);
                if(stack.isEmpty())return;
                temp=stack.pop();
            }
            stack.add(temp);
            temp = temp.right;
        }
    }

4、morris遍歷是一種很神奇的遍歷,上述的三種方法和遞歸遍歷方法都需要O(n)的時間,O(n)的空間,而morris遍歷則只需要O(n)的時間,O(1)的空間。下面以morris中序遍歷來說明。
算法僞代碼如下:
MorrisInOrder():
while 沒有結束
如果當前節點沒有左孩子
訪問該節點
轉向該節點的右孩子
否則
找到左孩子的最右節點。
如果最右節點的右孩子不爲空則說明最右節點已和當前節點連接,訪問當前節點,並把最右節點的只有孩子置爲空,轉向當前節點的右孩子。
如果最右節點的右孩子爲空,則使最右節點的右孩子指向當前節點,轉向當前節點的左孩子。

public static void morrisInOrder(TreeNode root){
        TreeNode cur=root;
        while(cur!=null){
            if(cur.left==null){
                System.out.println(cur.val);
                cur=cur.right;
            }else{
                TreeNode temp=cur.left;
                while(temp.right!=null&&temp.right!=cur){
                    temp=temp.right;
                }
                if(temp.right==cur){
                    System.out.println(cur.val);
                    temp.right=null;
                    cur=cur.right;
                }else{
                    temp.right=cur;
                    cur=cur.left;
                }
            }
        }
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章