Chinaunix首页 | 论坛 | 博客
  • 博客访问: 256093
  • 博文数量: 170
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1709
  • 用 户 组: 普通用户
  • 注册时间: 2014-05-06 18:01
文章分类

全部博文(170)

文章存档

2016年(11)

2015年(130)

2014年(29)

分类: Java

2015-09-26 15:20:24

//Validate Binary Search Tree My Submissions Question Solution 
//Total Accepted: 63004 Total Submissions: 312589 Difficulty: Medium
//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 ValidateBinarySearchTree {


public static void main(String[] args) {
// TODO Auto-generated method stub


}
TreeNode pre = null;  
public boolean isValidBST(TreeNode root) {
if(root==null)
    return true;
  if(!isValidBST(root.left))
  return false;
  if(pre!=null&&pre.val>=root.val)
  return false;
  pre=root;
  return isValidBST(root.right);
  
}



}

阅读(739) | 评论(0) | 转发(0) |
0

上一篇:SymmetricTree

下一篇:NumberofDigitOne

给主人留下些什么吧!~~