98. Validate Binary Search Tree

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.
Example 1:
Input:
    2
   / \
  1   3
Output: true
Example 2:
    5
   / \
  1   4
     / \
    3   6
Output: false
Explanation: The input is: [5,1,4,null,null,3,6]. The root node's value
             is 5 but its right child's value is 4.
Analysis:
5/30/2018 Update:
The iterative inorder traversal approach is trivial. What is worth to know is the recursive divide and conquer approach. Details see code.

Solution:
class Solution {
    public boolean isValidBST(TreeNode root) {
        return helper(root, Long.MIN_VALUE, Long.MAX_VALUE);
    }
    
    private boolean helper(TreeNode root, long min, long max) {
        if (root == null)
            return true;
        if (root.val >= max || root.val <= min) {
            return false;
        }
        return helper(root.left, min, root.val) && helper(root.right, root.val, max);
    }
}


--------------------------------------------------------------------------------------------------------------------------------------------
12/29/2017 Update
No need to create return type helper class.

For this problem in-order traversal is a better approach. The nature of in-order is from left to root to right. If the tree is binary search tree, in-order's sequence follows ascending order.

Solution:
 /**  
  * Definition for a binary tree node.  
  * public class TreeNode {  
  *   int val;  
  *   TreeNode left;  
  *   TreeNode right;  
  *   TreeNode(int x) { val = x; }  
  * }  
  */  
 class Solution {  
   public boolean isValidBST(TreeNode root) {  
     if (root == null) return true;  
     Stack<TreeNode> stack = new Stack<>();  
     TreeNode prev = null;  
     while (!stack.isEmpty() || root!= null) {  
      if(root != null) {  
       // add to stack, turn left;  
       stack.push(root);  
       root = root.left;  
      } else {  
       root = stack.pop();  
       if (prev != null && root.val <= prev.val ) return false;  
       prev = root;  
       root = root.right;  
      }  
     }  
     return true;  
   }  
 }  
It's important to have prev as node instead of int to handle overflow. 
----------------------------------------------------------------------------------------------------------------

思路:
用divide and conquer。由于要root要大于left子树所有的值,要小于right子树所有的值。所以需要一个result type存储当前root下的max 和min value。

判断是否valid代码如下:

  if(!left.isValid || !right.isValid)  
       return new RT(false, 0 , 0);  
 if(root.left!=null && left.max>= root.val ||  
       root.right !=null && right.min <= root.val)  
       {  
         return new RT(false, 0,0);  
       }  



实现:
判断违规的时候要把=加进去。

评论

此博客中的热门博文

776. Split BST

663. Equal Tree Partition

532. K-diff Pairs in an Array