Problem 98: Validate Binary Search Tree

https://leetcode.com/problems/validate-binary-search-tree/

思路

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public boolean isValidBST(TreeNode root) {
        if (root == null) {
            return true;
        }
        Stack<TreeNode> stack = new Stack<>();
        TreeNode prev = null;
        while (root != null || !stack.isEmpty()) {
            while (root != null) {
                stack.push(root);
                root = root.left;
            }
            root = stack.pop();
            if (prev != null && prev.val >= root.val) return false;
            prev = root;
            root = root.right;
        }

        return true;
    }
}
  • Solution 2: 递归

  • Solution 3: 分治

  • 分治思想,有几个点很容易出错;

  • 这里要维护几个不同的值:min, max 和 is_bst,所以直接创立一个 ResultType 的类是比较方便的

  • 关键点:左边的最大要小于root,右边的最小要小于root。这样才能成为一个 binary search tree.

易错点

  1. root为空的情况。

  2. 这里的 min 和 max 跟 ResultType 里正好是相反的!原因是为了能不停地迭代,要判断 max,那开始就设成 min,这样能不停地往下走。

  3. root 为空,不代表整个树不是搜索树,所以空结点要返回一个 true;另外一方面,这个空节点的最大最小值不能影响整个树的结构,所以我们把最小值设成最大,最大值设成最小。

  4. 条件判断,非常易错

    注意看这里是root.left != nullleft.max >= root.val! 而我错就错在写成了left.max >= root.val!这种情况下,当 left 本身为空的时候,直接就会出错。

  5. 指代对象不同

    这里是root.left,和left两个东西left指的是 ResultType;而root.left是判断树的左边有没有东西!

Last updated

Was this helpful?