题目

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. php模板引擎之featherview

    在纯php文件中不加php结束符是一个好习惯,php结束符仅用于在php与html混写时标示php代码结束. <? ?>是短标签,<?php ?>是长标签,在php的配置文件( ...

  2. Ubuntu 安装Chrome

    apt方式安装Chrome 1.添加密匙 wget -q -O - https://dl.google.com/linux/linux_signing_key.pub | sudo apt-key a ...

  3. 【FFT&NTT 总结】

    $FFT$总结 (因为还不会啊,,都没做过什么题,所以一边学一边打咯.. 1.主要是用来加速卷积形式的求和吧? $F*G(n)=F[i] × G[n-i]$ 平时是$O(n^2)$的,FFT可以$O( ...

  4. curl请求指定host ip(指定域名解析的内网某ip)

    域名www.test.com解析内部多台ip $httpHeader = array('Host: www.test.com');$url = "10.17.2.245/xxx/xxx/t. ...

  5. Develop with asyncio部分的翻译

    Develop with asyncio 异步程序和普通的连续程序(也就是同步程序)是很不一样的,这里会列出一些常见的陷阱,并介绍如何去避开他们. Debug mode of asyncio 我们用a ...

  6. spring data redis的配置类RedisConfig

    package com.tz.config; import org.springframework.context.annotation.Bean; import org.springframewor ...

  7. httpclient的并发连接问题

    昨天的搜索系统又出状况了,几个库同时重建索引变得死慢.经过一个上午的复现分析,确定问题出现httpclient的使用上(我使用的是3.1这个被广泛使用的遗留版本).搜索系统在重建索引时,是并发多个线程 ...

  8. [苹果]苹果AppStore应用审核标准

    [苹果]苹果AppStore应用审核标准 http://wenku.baidu.com/view/a9152d2c647d27284b7351a1.html   苹果app审核指南 http://we ...

  9. Serilog中的Jobject/Jtoken对象序列化的问题

    今天使用Serilog打印object对象的时候,发现Jtoken对象输出成 [[[]] 这种形式了,本来以为是传入参数的问题,确认了几遍后发现确实是Serilog输出的问题.github上也有人提出 ...

  10. linux下授予某用户对某文件夹的读写权限