0098. Validate Binary Search Tree

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

Description

Given the root of a binary tree, determine if it is a valid binary search tree (BST).

A valid 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:

Example 2:

Constraints:

  • The number of nodes in the tree is in the range [1, 104].

  • -231 <= Node.val <= 231 - 1

ac1: inorder traversal

  • BST, inorder traversal is transcendent.

  • Traversal: consider three part: left,right and itself, it's about order.

ac2: iterative, same as traversal

in this case, iteration is easier to understand.

First answer, so ugly, must be wrong.

ac3: recursive

becareful about the boudary, especially Integer.MAX_VALUE Integer.MIN_VALUE

2/18/2018

Last updated

Was this helpful?