98. 验证二叉搜索树

2021/12/7 6:21:23

本文主要是介绍98. 验证二叉搜索树,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

给你一个二叉树的根节点 root ,判断其是否是一个有效的二叉搜索树。

有效 二叉搜索树定义如下:

节点的左子树只包含 小于 当前节点的数。
节点的右子树只包含 大于 当前节点的数。
所有左子树和右子树自身必须也是二叉搜索树。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/validate-binary-search-tree
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

递归

class Solution {
    private static Info solve(TreeNode root) {
        if (root == null) {
            return new Info(true, null, null);
        }
        Info left = solve(root.left);
        Info right = solve(root.right);

        return new Info(left.isValidBST && right.isValidBST &&
                (left.mostRight == null || left.mostRight.val < root.val) && (right.mostLeft == null || right.mostLeft.val > root.val),
                left.mostLeft == null ? root : left.mostLeft, right.mostRight == null ? root : right.mostRight);
    }

    public static boolean isValidBST(TreeNode root) {
        if (root == null) {
            return true;
        }
        return solve(root).isValidBST;
    }

    public static void main(String[] args) {
        TreeNode root = new TreeNode(5);
        TreeNode n1 = new TreeNode(1);
        TreeNode n2 = new TreeNode(4);
        TreeNode n3 = new TreeNode(3);
        TreeNode n4 = new TreeNode(6);

        root.left = n1;
        root.right = n2;
        n2.left = n3;
        n2.right = n4;
        System.out.println(isValidBST(root));
    }
}

class Info {
    boolean isValidBST;
    TreeNode mostLeft;
    TreeNode mostRight;

    public Info(boolean isValidBST, TreeNode mostLeft, TreeNode mostRight) {
        this.isValidBST = isValidBST;
        this.mostLeft = mostLeft;
        this.mostRight = mostRight;
    }
}


class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;

    TreeNode() {
    }

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

    TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}

中序遍历

class Solution {
    public static boolean isValidBST(TreeNode root) {
        if (root == null) {
            return true;
        }
        TreeNode pre = null, cur = root;

        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;
                    continue;
                } else {
                    mostRight.right = null;
                }
            }
            if (pre != null && pre.val >= cur.val) {
                return false;
            }

            pre = cur;
            cur = cur.right;
        }
        return true;
    }
}

class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;

    TreeNode() {
    }

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

    TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}


这篇关于98. 验证二叉搜索树的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程