http://www.lintcode.com/zh-cn/problem/validate-binary-search-tree/#

错误点:1. helper函数里先要判断 if(!left.isBst || !right.isBst)

    2. minVal 和maxVal的选择要选好。

注意点: 平衡二叉查找树  balanced BST   增删改查复杂度 O(n)

     BST 复杂度O(h)  h= 二叉树深度

类似中序遍历的解法:

(中序遍历)  

ArrayList<Integer> result = new ArrayList<Integer>();
public ArrayList<Integer> inorderTraversal(TreeNode root) {
if(root == null) return result;
inorderTraversal(root.left);
result.add(root.val);
inorderTraversal(root.right);
return result;
}

 1 public boolean isValidBST(TreeNode root) { 
 2     if(root == null) return true;
 3     if(root.left != null && root.val <= root.left.val) return false;
 4     if(!isValidBST(root.left)) return false;
 5     if(root.right != null){
 6         if(root.val >= root.right.val) return false;
 7     }
 8     if(!isValidBST(root.right)) return false;
 9     return true;
10     10
11   5   15
12      6    20
13 }
14 //-----------------------------------------------------
15 private TreeNode lastNode = null;
16 public boolean isValidBST(TreeNode root) { 
17     if(root == null) return true;
18     if(!isValidBST(root.left)) return false;
19     if(lastNode !=null && root.val <= lastNode.val) return false;
20     lastNode = root;
21     if(!isValidBST(root.right)) return false;
22     return true;
23 }
24 //-------------------------------------------------------------------
25 class ResultType {
26     private int minVal, maxVal;
27     private boolean isBst;
28     public ResultType(boolean isBst, int maxVal, int minVal) {
29         this.isBst = isBst;
30         this.maxVal = maxVal;
31         this.minVal = minVal;
32     }
33 }
34 public boolean isValidBST(TreeNode root) { 
35     if(root == null) return true;
36     return helper(root).isBst;
37     
38 }
39 private ResultType helper(TreeNode root) {
40     if(root == null) return new ResultType(true, Integer.MIN_VALUE, Integer.MAX_VALUE);
41     ResultType left = helper(root.left);
42     ResultType right = helper(root.right);
43     if(!left.isBst) return new ResultType(false, 0, 0);
44     if(!right.isBst) return new ResultType(false, 0, 0);
45     int minVal = Math.min(root.val,left.minVal);
46     int maxVal = Math.max(root.val, right.maxVal);
47     if(root.left != null && root.val <= left.maxVal || root.right != null && root.val >= right.minVal)
48         return new ResultType(false, 0, 0);
49     return new ResultType(true, maxVal, minVal);
50 }
5-24  第一个方法是错误的

 

 1  int lastVal = Integer.MIN_VALUE;
 2  boolean firstNode = true;      //就是为了排除就1个结点,且值为MIN_VALUE的情况。
 3  public boolean isValidBST(TreeNode root) {
 4      if(root == null) return true;
 5      if(!isValidBST(root.left)) return false;   //相当于从上一直往左下遍历,遍历到头,相当于已经走过了左子和root,就是中序遍历的过程
 6      if(!firstNode && lastVal >= root.val) return false;
 7      firstNode = false;
 8      lastVal = root.val;
 9      if(!isValidBST(root.right)) return false;
10      return true;
11  }
View Code

分治

 1 class ResultType {
 2      boolean isBST;
 3      int minVal, maxVal;
 4      public ResultType(boolean isBST, int minVal, int maxVal) {
 5          this.isBST = isBST;
 6          this.minVal = minVal;
 7          this.maxVal = maxVal;     
 8      }
 9  }
10  private ResultType helper(TreeNode root) {
11      if(root == null) {
12          return new ResultType(true, Integer.MAX_VALUE, Integer.MIN_VALUE);  //空结点,不要对非空结点的max,min值产生影响。
13      }
14      ResultType left = helper(root.left);
15      ResultType right = helper(root.right);
16      if(!left.isBST || !right.isBST) {
17          return new ResultType(false, 0, 0); //已经是false了,minVal 和maxVal取什么值无所谓,反正不会用它去比较。
18      }
19      if(root.left!= null &&left.maxVal >= root.val || root.right != null &&right.minVal <= root.val) return new ResultType(false, 0, 0);
20      return new ResultType(true, Math.min(root.val, left.minVal), 
21                                  Math.max(root.val, right.maxVal));
22  }
23   public boolean isValidBST(TreeNode root) {
24     // write your code here
25     return helper(root).isBST;
26 }
27     
28 //第一次的想法,if条件设太多了,应该逐层考虑,不然各种条件分情况分不过来。-------------------------
29     private void helper(TreeNode root) {
30         if(root  == null || (root.left ==null && root.right == null) ) return true;
31         if(root.left == null){
32             if(root.val >= root.right.val) return false;
33         }
34         if(root.right == null){
35             if(root.val <= root.left.val) return false;
36         }
37         if(root.right != null & root.left != null) {
38             if(!(root.val > root.left.val && root.val < root.right.val)) return false;
39         }
40         return helper(root.left ) && helper(root.right);    
41     }
View Code