题目

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.

题解:

题目非常善良的给了binary search tree的定义。

这道题就是判断当前树是不是BST,所以递归求解就好。

第一种方法是中序遍历法。

因为如果是BST的话,中序遍历数一定是单调递增的,如果违反了这个规律,就返回false。

代码如下:

 1 public boolean isValidBST(TreeNode root) {  
 2     ArrayList<Integer> pre = new ArrayList<Integer>();  
 3     pre.add(null);  
 4     return helper(root, pre);  
 5 }  
 6 private boolean helper(TreeNode root, ArrayList<Integer> pre)  
 7 {  
 8     if(root == null)  
 9         return true; 
     
     boolean left = helper(root.left,pre); 
     
     if(pre.get(pre.size()-1)!=null && root.val<=pre.get(pre.size()-1))  
         return false;  
     pre.add(root.val);  
     
     boolean right = helper(root.right,pre);
     return left && right;  
 }

第二种方法是直接按照定义递归求解。

“根据题目中的定义来实现,其实就是对于每个结点保存左右界,也就是保证结点满足它的左子树的每个结点比当前结点值小,右子树的每个结点比当前结
点值大。对于根节点不用定位界,所以是无穷小到无穷大,接下来当我们往左边走时,上界就变成当前结点的值,下界不变,而往右边走时,下界则变成当前结点
值,上界不变。如果在递归中遇到结点值超越了自己的上下界,则返回false,否则返回左右子树的结果。”

代码如下:

 1     public boolean isValidBST(TreeNode root) {  
 2         return isBST(root, Integer.MIN_VALUE, Integer.MAX_VALUE);
 3     }  
 4       
 5     public boolean isBST(TreeNode node, int low, int high){  
 6         if(node == null)  
 7             return true;  
 8             
 9         if(low < node.val && node.val < high)
             return isBST(node.left, low, node.val) && isBST(node.right, node.val, high);  
         else  
             return false;  
     } 

Reference:http://blog.csdn.net/linhuanmars/article/details/23810735

Validate Binary Search Tree leetcode java的更多相关文章

  1. Validate Binary Search Tree [LeetCode]

    Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...

  2. Validate Binary Search Tree——LeetCode

    Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...

  3. Recover Binary Search Tree leetcode java

    题目: Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without chan ...

  4. Convert Sorted Array to Binary Search Tree leetcode java

    题目: Given an array where elements are sorted in ascending order, convert it to a height balanced BST ...

  5. Convert Sorted List to Binary Search Tree leetcode java

    题目: Given a singly linked list where elements are sorted in ascending order, convert it to a height ...

  6. Leetcode 笔记 98 - Validate Binary Search Tree

    题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...

  7. LeetCode: Validate Binary Search Tree 解题报告

    Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...

  8. 【leetcode】Validate Binary Search Tree

    Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...

  9. 【LeetCode练习题】Validate Binary Search Tree

    Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...

随机推荐

  1. BZOJ3500 : PA2008 Cliquers

    设g[i]表示n=i时的答案,则OEIS上可以找到如下递推式: g[i]=g[i-1]+g[i-2]-g[i-5]-g[i-7]+... 其中符号为++--交替,第i项为f[i],f[1]=1,f[2 ...

  2. xenserver系列之安装与配置

    文章内容 1 ,安装 一.选择键盘类型:US. 二.这个是欢迎界面,选择“OK”即可. 三.这个是使用协议,我们必须得接受,否则就不能继续安装了! 四.这个是警告,告诉你XenServer所需要的硬件 ...

  3. Centos部署使用Jexus承载asp.net core2 web应用

    一,首先安装本地开发项目用的的 core对应版本运行时: https://www.microsoft.com/net/download/linux-package-manager/centos/run ...

  4. Docker系列之(一):10分钟玩转Docker

    1.前言 进入云计算的时代,各大云提供商AWS,阿里云纷纷推出针对Docker的服务,现在Docker是十分火爆,那么Docker到底是什麽,让我们来体验一下. 2.Docker是什麽 Docker是 ...

  5. Both - Either - Neither English Grammar

    http://www.grammar.cl/english/both-either-neither.htm Both, Either, Neither Summary Chart We use bot ...

  6. GO语言基础之method

    方法 method 1. Go 中虽没有 class,但依旧有 method 2. 通过显示说明 receiver 来实现与某个类型的组合 3. 只能为同一个包中的类型定义方法 4. Receiver ...

  7. CentOS快速安装最新版本的SaltStack

    SaltStack是一个类似Puppet的自动运维管理工具,打算用它来管理实验室的所有服务器包括OpenStack,当然要想把它玩起来首先得把它安装上, 这里我写了一个简单的脚本来在CentOS下快速 ...

  8. JavaScript学习总结(二十)——Javascript非构造函数的继承

    一.什么是"非构造函数"的继承? 比如,现在有一个对象,叫做"中国人". var Chinese = { nation:'中国' }; 还有一个对象,叫做&qu ...

  9. SpringBoot打jar包问题

    原文:https://jingyan.baidu.com/article/6f2f55a11d6e09b5b93e6c9e.html 当你使用springBoot进行打包的时候,这篇经验会帮助到你的. ...

  10. 在xcode5下设置两个viewController跳转——关键是禁用arc

    1.禁用arc 2.然后使用如下代码: - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:( ...