Morris遍历

题目链接:力扣

方法一:DFS

        实现思路:递归算出每一个节点为头节点的树的最大值和最小值,以及它是否是二叉搜索树。根节点满足左子树的最大值小于自己、右子树的最大值大于自己,且左右子树都是二叉搜索树,返回true。直接看实现代码,重点说Morris遍历:

    public boolean isValidBST(TreeNode root) {
        return method(root).isBSt;
    }
    
    public static class Info {
        private boolean isBSt;
        private int max;
        private int min;

        public Info(boolean isBSt, int max, int min) {
            this.isBSt = isBSt;
            this.max = max;
            this.min = min;
        }
    }

    public Info method(TreeNode node) {
        if (node == null) {
            return null;
        }
        Info leftInfo = method(node.left);
        Info rightInfo = method(node.right);
        int max = node.val;
        int min = node.val;
        if (leftInfo != null) {
            max = Math.max(leftInfo.max, max);
        }
        if (rightInfo != null) {
            max = Math.max(rightInfo.max, max);
        }
        if (leftInfo != null) {
            min = Math.min(leftInfo.min, min);
        }
        if (rightInfo != null) {
            min = Math.min(rightInfo.min, min);
        }
        boolean isBSt = true;
        if (leftInfo != null && !leftInfo.isBSt) {
            isBSt = false;
        }
        if (rightInfo != null && !rightInfo.isBSt) {
            isBSt = false;
        }
        if (leftInfo != null && leftInfo.max >= node.val) {
            isBSt = false;
        }
        if (rightInfo != null && rightInfo.min <= node.val) {
            isBSt = false;
        }
        return new Info(isBSt, max, min);
    }

之前的方法(递归、按层遍历)遍历二叉树虽然代码简洁,但不是最优,Morris遍历能做到空间复杂度O(1)。

Morris:时间复杂度O(n)、空间复杂度O(1);利用叶子节点的空闲指针实现

递归:时间复杂度O(n)、空间复杂度O(height),height是二叉树的最大高度

按层遍历:时间复杂度O(n)、空间复杂度O(width),width是二叉树的的最大宽度

Morris遍历实现步骤:

        假设遍历到当前节点为cur:

        1.若cur节点没有左子树,那么直接去cur的右子树(cur=cur.right)

        2.若cur节点有左子树,找到左子树的最右节点mostRight:

                1)mostRight的右指针指向null,那么让其指向cur节点,然后cur节点左移(cur=cur.left)

                2)mostRight的右指针指向cur,那么让其指向null(说明是第二次来到cur这个节点),然后cur右移(cur=cur.right)

        3.直到cur为空遍历完成

Morris遍历实质:利用左子树的最右节点mostRight的右指针是否指向为null,判断当前cur节点是否来到过。

    public static void morris(TreeNode root) {
        if (root == null) {
            return;
        }
        TreeNode cur = root;
        TreeNode mostRight = null;
        while (cur != null) {
            mostRight = cur.left;//来到当前节点的左树节点
            if (mostRight != null) {//如果左树节点不为空
                while (mostRight.right != null && mostRight.right != cur) {
                    mostRight = mostRight.right;//找到左子树的最右节点
                }
                if (mostRight.right == null) {
                    mostRight.right = cur;
                    cur = cur.left;
                    continue;
                } else {
                    mostRight.right = null;
                }
            }
            //左树节点为空:cur向right移动
            cur = cur.right;
        }
    }

 说完Morris遍历怎么解决这个题?

         判断是否是二叉搜索树的关键就是看中序遍历结果是否递增。则Morris遍历的到中序遍历的结果即可。因为在Morris遍历中,遍历到的当前节点有左子树,那么这个节点必定会到来两次,也就是说对于没有左子树的节点,遍历到就处理,有左子树的节点第二次到它才做处理。

    public static List<Integer> process(TreeNode node) {
        List<Integer> list = new ArrayList<>();
        if (node == null) {
            return list;
        }
        TreeNode cur = node;
        while (cur != null) {
            TreeNode mostRight = cur.left;
            if (mostRight != null) {
                while (mostRight.right != null && mostRight.right != cur) {
                    mostRight = mostRight.right;
                }
                if (mostRight.right == null) {
                    mostRight.right = cur;
                    cur = cur.left;
                } else {
                    mostRight.right = null;
                    list.add(cur.val);//第二次到cur才处理
                    cur = cur.right;
                }
            } else {//当前左子树为空,cur不会来第二次
                list.add(cur.val);
                cur = cur.right;
            }
        }
        return list;
    }

Morris遍历解决判断是否是二叉搜索树的整体代码:

    /*
     * Morris遍历判断是否是搜索二叉树
     * */
    public static boolean isValidBST1(TreeNode root) {
        List<Integer> list = process(root);
        for (int i = 0; i < list.size() - 1; i++) {
            if (list.get(i) >= list.get(i + 1)) {
                return false;
            }
        }
        return true;
    }

    public static List<Integer> process(TreeNode node) {
        List<Integer> list = new ArrayList<>();
        if (node == null) {
            return list;
        }
        TreeNode cur = node;
        while (cur != null) {
            TreeNode mostRight = cur.left;
            if (mostRight != null) {
                while (mostRight.right != null && mostRight.right != cur) {
                    mostRight = mostRight.right;
                }
                if (mostRight.right == null) {
                    mostRight.right = cur;
                    cur = cur.left;
                } else {
                    mostRight.right = null;
                    list.add(cur.val);//第二次到cur才处理
                    cur = cur.right;
                }
            } else {//当前左子树为空,cur不会来第二次
                list.add(cur.val);
                cur = cur.right;
            }
        }
        return list;
    }

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值