Leetcode98. Validate Binary Search Tree

Leetcode98. Validate Binary Search Tree

Given a binary tree, determine if it is a valid binary search tree (BST).
Assume a BST is defined as follows:

  • The left subtree of a node contains only nodes with keys less than the node’s key.
  • The right subtree of a node contains only nodes with keys greater than the node’s key.
  • Both the left and right subtrees must also be binary search trees.

Example 1:

    2
   / \
  1   3

Input: [2,1,3]
Output: true

Example 2:

    5
   / \
  1   4
     / \
    3   6

Input: [5,1,4,null,null,3,6]
Output: false
Explanation: The root node's value is 5 but its right child's value is 4.

解法一 判断根节点是否合法

左子树是合法二分查找树,右子树是合法二分查找树,并且根节点大于左子树中最大的数,小于右子树中最小的数,那么当前树就是合法二分查找树。

不应该由「根节点大于左孩子,小于右孩子」来判断!

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
 
public boolean isValidBST(TreeNode root) {
    if (root == null || root.left == null && root.right == null) {
        return true;
    }
    //左子树是否合法
    if (isValidBST(root.left)) {
        if (root.left != null) {
            int max = getMaxOfBST(root.left);//得到左子树中最大的数
            if (root.val <= max) { //相等的情况,代表有重复的数字
                return false;
            }
        }

    } else {
        return false;
    }

    //右子树是否合法
    if (isValidBST(root.right)) {
        if (root.right != null) {
            int min = getMinOfBST(root.right);//得到右子树中最小的数
            if (root.val >= min) { //相等的情况,代表有重复的数字
                return false;
            }
        }

    } else {
        return false;
    }
    return true;
}

private int getMinOfBST(TreeNode root) {
    int min = root.val;
    while (root != null) {
        if (root.val <= min) {
            min = root.val;
        }
        root = root.left;
    }
    return min;
}

private int getMaxOfBST(TreeNode root) {
    int max = root.val;
    while (root != null) {
        if (root.val >= max) {
            max = root.val;
        }
        root = root.right;
    }
    return max;
}

解法二 判断左右孩子是否合法

解法一中,我们是判断根节点是否合法,找到了左子树中最大的数,右子树中最小的数。 由左子树和右子树决定当前根节点是否合法。

但如果正常的来讲,明明先有的根节点,按理说根节点是任何数都行,而不是由左子树和右子树限定。相反,根节点反而决定了左孩子和右孩子的合法取值范围。

所以,我们可以从根节点进行 DFS,然后计算每个节点应该的取值范围,如果当前节点不符合就返回 false。

	  10
    /    \
   5     15
  / \    /  
 3   6  7 

   考虑 10 的范围
     10(-inf,+inf)

   考虑 5 的范围
     10(-inf,+inf)
    /
   5(-inf,10)

   考虑 3 的范围
       10(-inf,+inf)
      /
   5(-inf,10)
    /
  3(-inf,5)  

   考虑 6 的范围
       10(-inf,+inf)
      /
   5(-inf,10)
    /       \
  3(-inf,5)  6(5,10)

   考虑 15 的范围
      10(-inf,+inf)
    /          \
    5(-inf,10) 15(10,+inf)
    /       \
  3(-inf,5)  6(5,10)  

   考虑 7 的范围,出现不符合返回 false
       10(-inf,+inf)
     /              \
5(-inf,10)           15(10,+inf)
  /       \             /
3(-inf,5)  6(5,10)   7(10,15)

可以观察到,左孩子的范围是 (父结点左边界,父节点的值),右孩子的范围是(父节点的值,父节点的右边界)。

扫描二维码关注公众号,回复: 9254115 查看本文章

但是java 里边没有提供负无穷和正无穷,如果表示inf:

  1. 假设我们的题目的数值都是 Integer 范围的,那么我们用不在 Integer 范围的数字来表示负无穷和正无穷。用 long 去存储。
  2. 传入 Integer 对象,然后 null 表示负无穷和正无穷。然后利用 JAVA 的自动装箱拆箱,数值的比较可以直接用不等号。
public boolean isValidBST(TreeNode root) {
    long maxValue = (long)Integer.MAX_VALUE + 1;
    long minValue = (long)Integer.MIN_VALUE - 1;
    return getAns(root, minValue, maxValue);
}

private boolean getAns(TreeNode node, long minVal, long maxVal) {
    if (node == null) {
        return true;
    }
    if (node.val <= minVal) {
        return false;
    }
    if (node.val >= maxVal) {
        return false;
    }
    return getAns(node.left, minVal, node.val) && getAns(node.right, node.val, maxVal);
}
public boolean isValidBST(TreeNode root) {
    return getAns(root, null, null);
}

private boolean getAns(TreeNode node, Integer minValue, Integer maxValue) { 
    if (node == null) {
        return true;
    }
    if (minValue != null && node.val <= minValue) {
        return false;
    }
    if (maxValue != null && node.val >= maxValue) {
        return false;
    }
    return getAns(node.left, minValue, node.val) && getAns(node.right, node.val, maxValue);
}

解法三 DFS BFS

解法二就是树的先序遍历,这里用栈来模拟解法二的递归过程。

public boolean isValidBST(TreeNode root) {
    if (root == null || root.left == null && root.right == null) {
        return true;
    }
    //利用三个栈来保存对应的节点和区间
    LinkedList<TreeNode> stack = new LinkedList<>();
    LinkedList<Integer> minValues = new LinkedList<>();
    LinkedList<Integer> maxValues = new LinkedList<>();
    //头结点入栈
    TreeNode pNode = root;
    stack.push(pNode);
    minValues.push(null);
    maxValues.push(null);
    while (pNode != null || !stack.isEmpty()) {
        if (pNode != null) {
            //判断栈顶元素是否符合
            Integer minValue = minValues.peek();
            Integer maxValue = maxValues.peek();
            TreeNode node = stack.peek();
            if (minValue != null && node.val <= minValue) {
                return false;
            }
            if (maxValue != null && node.val >= maxValue) {
                return false;
            }
            //将左孩子加入到栈
            if(pNode.left!=null){
                stack.push(pNode.left);
                minValues.push(minValue);
                maxValues.push(pNode.val);
            }

            pNode = pNode.left;
        } else { // pNode == null && !stack.isEmpty()
            //出栈,将右孩子加入栈中
            TreeNode node = stack.pop();
            minValues.pop();
            Integer maxValue = maxValues.pop();
            if(node.right!=null){
                stack.push(node.right);
                minValues.push(node.val);
                maxValues.push(maxValue);
            }
            pNode = node.right;
        }
    }
    return true;
}

上边的 DFS 可以看出来一个缺点,就是我们判断完当前元素后并没有出栈,后续还会回来得到右孩子后才会出栈。所以其实我们可以用 BFS,利用一个队列,一层一层的遍历,遍历完一个就删除一个。

public boolean isValidBST(TreeNode root) {
    if (root == null || root.left == null && root.right == null) {
        return true;
    }
    //利用三个队列来保存对应的节点和区间
    Queue<TreeNode> queue = new LinkedList<>();
    Queue<Integer> minValues = new LinkedList<>();
    Queue<Integer> maxValues = new LinkedList<>();
    //头结点入队列
    TreeNode pNode = root;
    queue.offer(pNode);
    minValues.offer(null);
    maxValues.offer(null);
    while (!queue.isEmpty()) {
        //判断队列的头元素是否符合条件并且出队列
        Integer minValue = minValues.poll();
        Integer maxValue = maxValues.poll();
        pNode = queue.poll();
        if (minValue != null && pNode.val <= minValue) {
            return false;
        }
        if (maxValue != null && pNode.val >= maxValue) {
            return false;
        }
        //左孩子入队列
        if(pNode.left!=null){
            queue.offer(pNode.left);
            minValues.offer(minValue);
            maxValues.offer(pNode.val);
        }
        //右孩子入队列
        if(pNode.right!=null){
            queue.offer(pNode.right);
            minValues.offer(pNode.val);
            maxValues.offer(maxValue);
        } 
    }
    return true;
}

解法四 中序遍历

中序遍历顺序会是左孩子,根节点,右孩子。二分查找树的性质,左孩子小于根节点,根节点小于右孩子。

是的,如果我们将中序遍历的结果输出,那么将会到的一个从小到大排列的序列。

所以我们只需要进行一次中序遍历,将遍历结果保存,然后判断该数组是否是从小到大排列的即可。

更近一步,由于我们只需要临近的两个数的相对关系,所以我们只需要在遍历过程中,把当前遍历的结果和上一个结果比较即可。

public boolean isValidBST(TreeNode root) {
    if (root == null) return true;
    Stack<TreeNode> stack = new Stack<>();
    TreeNode pre = null;
    while (root != null || !stack.isEmpty()) {
        while (root != null) {
            stack.push(root);
            root = root.left;
        }
        root = stack.pop();
        if(pre != null && root.val <= pre.val) return false;
        pre = root;
        root = root.right;
    }
    return true;
}

迭代的中序遍历来解决多个问题

发布了82 篇原创文章 · 获赞 7 · 访问量 5005

猜你喜欢

转载自blog.csdn.net/magic_jiayu/article/details/104274243
今日推荐