-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathSolution.java
More file actions
45 lines (39 loc) · 1.21 KB
/
Solution.java
File metadata and controls
45 lines (39 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package ValidateBinarySearchTree;
import commons.datastructures.TreeNode;
/**
* User: Danyang
* Date: 1/27/2015
* Time: 20:52
*
* 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.
*/
public class Solution {
/**
* Complexity appears to be O(n lgn), but actually O(n)
* @param root
* @return
*/
public boolean isValidBST(TreeNode root) {
if(root==null)
return true;
TreeNode l = getMax(root.left);
TreeNode r = getMin(root.right);
if(l!=null && l.val>=root.val || r!=null && r.val<=root.val)
return false;
return isValidBST(root.left) && isValidBST(root.right);
}
TreeNode getMax(TreeNode cur) {
while(cur!=null && cur.right!=null)
cur = cur.right;
return cur;
}
TreeNode getMin(TreeNode cur) {
while(cur!=null && cur.left!=null)
cur = cur.left;
return cur;
}
}