Problem:
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.
confused what "{1,#,2,3}"
means?
OJ’s Binary Tree Serialization:The serialization of a binary tree follows a level order traversal, where ‘#’ signifies a path terminator where no node exists below.Here’s an example:
1
/ \
2 3
/
4
\
5
The above binary tree is serialized as "{1,2,3,#,#,4,#,#,5}"
.
Method 0:
Brute force. O(n^2).
Code for method 0:
/**
* 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;
else return isLess(root.left, root.val)&& isGreat(root.right, root.val)
&& isValidBST(root.left)&& isValidBST(root.right);
}
public boolean isLess(TreeNode root, int i){
if (root == null) return true;
if (root.val < i){
return isLess(root.left, i) && isLess(root.right, i);
}else return false;
}
public boolean isGreat(TreeNode root, int i){
if (root == null) return true;
if (root.val > i){
return isGreat(root.left, i) && isGreat(root.right, i);
}else return false;
}
}
Method 1:
- O(n).
- Possible mistake: Can not use Recursive. Remember, for a BST, everything on the right can not be larger than the root, not only for root.right. Same for the left.
- First, we could simply view it as a in-order traversal problem. However, we do need an external space to record the current min for the previous all steps.
Code for method 1:
/**
* 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) {
ArrayList<Integer> pre = new ArrayList<Integer>();// to store the max value
pre.add(null);
return helper(root,pre);
}
public boolean helper (TreeNode root, ArrayList<Integer> pre){
if (root == null) return true;
boolean left = helper(root.left, pre);
if (left == false) return false;
if (pre.get(0) !=null && root.val <= pre.get(0)) return false;
pre.set(0,root.val);
return helper(root.right, pre);
}
}
Method 2:
- Remember an upper bound, and an lower bound along the binary search tree.
- If go to the left child, update the upper bound. If go to the right child, update the lower bound.
- Detail: have to be careful about exceeding Integer range. Have to use type long to pass the auto-checker.
- Detail of concept: Pre-order, In-order, and Post-order traversals all belong to depth-first traversal.
- O(n)
Code for method 2:
/**
* 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) {
return helper (root, Long.MIN_VALUE, Long.MAX_VALUE);
}
public boolean helper(TreeNode root, long min, long max){
if (root == null) return true;
if (root.val <= min || root.val >= max) return false;
else return helper(root.left, min, root.val) && helper(root.right, root.val, max);
}
}