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;
    }
}
    心之所向,素履以往 生如逆旅,一苇以航

 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号