Morris遍历二叉树

2019-06-06  本文已影响0人  topshi

前言

Morris遍历过程

假设遍历到当前节点cur


如上图的一棵二叉树,

综合以上遍历过程,走过的节点顺序为
10 - > 5 -> 3 -> 5 -> 7 -> 10 - > 15 - > 18
可以看出,如果一个节点有左子树,它会被经过两次,反之,只会经过一次(也可以理解为两次重叠)。因为有左子树,才能被左子树的最右节点的右指针指向,没有左子树自然没有,回不来。

Morris前序遍历

在递归版遍历二叉树时,所谓的前序、中序、后续遍历不过是在处理数据的时机不同。Morris遍历也是如此。
如果我们在第一次遍历到节点时就对节点进行操作,就是前序遍历了。
判断第一次来到节点cur的依据:

leetcode 144. 二叉树的前序遍历

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
//Morris先序遍历
class Solution {
    public List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> list = new ArrayList<Integer>();
        if(root == null) return list;
        
        TreeNode cur = root;
        TreeNode mostRight = null;
        while(cur != null){
            mostRight = cur.left;
            if(mostRight != null){
                //找到mostRight,如果是第二次来到cur,mostRight还可能指向cur
                while(mostRight.right != null && mostRight.right != cur){
                    mostRight = mostRight.right;
                }
                //如果mostRight.right == null,说明是第一次来到cur
                if(mostRight.right == null){
                    list.add(cur.val);
                    mostRight.right = cur;
                    cur = cur.left;
                    continue;
                //第二次来到`cur`,将mostRight.right还原
                }else{
                    mostRight.right = null;
                }
            //没有左子树
            }else{
                list.add(cur.val);
            }
            cur = cur.right;
        }
        return list;
    }
}

Morris中序遍历

中序遍历很简单,就是在cur指针向右移之前做操作。
leetcode 94. 二叉树的中序遍历

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> list = new ArrayList<Integer>();
        if(root == null) return list;
        
        TreeNode cur = root;
        TreeNode mostRight = null;
        while(cur != null){
            mostRight = cur.left;
            if(mostRight != null){
                //找到mostRight,如果是第二次来到cur,mostRight还可能指向cur
                while(mostRight.right != null && mostRight.right != cur){
                    mostRight = mostRight.right;
                }
                //如果mostRight.right == null,说明是第一次来到cur
                if(mostRight.right == null){
                    mostRight.right = cur;
                    cur = cur.left;
                    continue;
                //第二次来到`cur`,将mostRight.right还原
                }else{
                    mostRight.right = null;
                }
            }
            list.add(cur.val);
            cur = cur.right;
        }
        return list;
    }
}

Morris后序遍历

二叉树可以被右边界划分,如下图所示


二叉树的右边界逆序

TreeNode p = head.right;
        TreeNode q = head;
        head.right = null;
        //逆序
        while(p != null){
            TreeNode x = p.right;
            p.right = q;
            q = p;
            p = x;
        }

145. 二叉树的后序遍历

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public List<Integer> postorderTraversal(TreeNode root) {
        List<Integer> list = new ArrayList<Integer>();
        if(root == null) return list;
        
        TreeNode cur = root;
        TreeNode mostRight = null;
        while(cur != null){
            mostRight = cur.left;
            if(mostRight != null){
                //找到mostRight,如果是第二次来到cur,mostRight还可能指向cur
                while(mostRight.right != null && mostRight.right != cur){
                    mostRight = mostRight.right;
                }
                //如果mostRight.right == null,说明是第一次来到cur
                if(mostRight.right == null){
                    mostRight.right = cur;
                    cur = cur.left;
                    continue;
                //第二次来到`cur`,将mostRight.right还原
                }else{
                    mostRight.right = null;
                    printEdge(cur.left, list);
                }
            }
            cur = cur.right;
        }
        printEdge(root, list);
        return list;
    }
    private void printEdge(TreeNode head, List<Integer> list){
        TreeNode p = head.right;
        TreeNode q = head;
        head.right = null;
        //逆序
        while(p != null){
            TreeNode x = p.right;
            p.right = q;
            q = p;
            p = x;
        }
        p = q;
        while(p != null){
            list.add(p.val);
            p = p.right;
        }
        p = q.right;
        q.right = null;
        //逆回来
        while(p != null){
            TreeNode x = p.right;
            p.right = q;
            q = p;
            p = x;
        }
    }
}

复杂度分析

上一篇 下一篇

猜你喜欢

热点阅读