98. Validate Binary Search Tree
# medium
Last updated
Was this helpful?
# medium
Last updated
Was this helpful?
We need a helper function to return each subtree's minimum and maximun values.
helper function - max and min value in a subtree
isValid function - both of left and right subtrees are valid, left_max < root and right_min > root, then it's valid.
Compute inorder binary tree traversal, if whole list is ascending, then it's valid.
逆向思维,如果parent tree是BST,那么left sub tree的下限是左子树的low,上限是parent node.val. 那么right sub tree的下限是parent node.val,上限是右子树的up. 默认上限是无穷大,下限是无穷小。每个node遍历一遍,时间复杂度和空间度为