98. Validate Binary Search Tree

# medium

Solution 1:

  1. helper function - max and min value in a subtree

  2. isValid function - both of left and right subtrees are valid, left_max < root and right_min > root, then it's valid.

Solution 2:

Compute inorder binary tree traversal, if whole list is ascending, then it's valid.

Solution 3:

逆向思维,如果parent tree是BST,那么left sub tree的下限是左子树的low,上限是parent node.val. 那么right sub tree的下限是parent node.val,上限是右子树的up. 默认上限是无穷大,下限是无穷小。每个node遍历一遍,时间复杂度和空间度为 O(n)O(n)

Last updated

Was this helpful?