Validate Binary Search Tree

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

题目意思:

判断一棵二叉树是不是搜索二叉树。

搜索二叉树定义是:1,左子树的所有节点的值全都小于根节点。2,右子树的所有节点值全都大于根节点。3,左右子树本身也必须同时为搜索二叉树。

解题思路:

这题我一开始以为只需要根的左节点小,右节点大就满足二叉搜索树的条件了。其实我错了,在二叉树的定义中,要求左子树的所有节点的值都小于根节点的值,即不能出现左节点的右节点的值大于根节点值的这种情况。不过这样反而让问题变得简单了起来,因为有一种思路就是我们只需要中序遍历二叉树,如果是二叉搜索树,那么遍历得到的顺序一定是从小到大的顺序,我们只需要用一个vector来保存遍历的结果,然后检查这个vector是不是升序就行了。

关于如何遍历二叉树,有前序,中序,后序三种遍历方式,其中每一种遍历还包括递归和循环两种实现。

递归遍历很简单,三种方式不同之处只体现在对于根节点的处理是在什么位置。而循环遍历稍微复杂一点,需要自己维护一个栈来模拟函数递归调用,三种方式中,前序和中序较为简单,后序比较困难一点,因为需要用一个额外的变量来记录根节点是在左节点结束时访问到的还是右节点结束时访问到的,我们只有在后者的情况下才去访问根节点。

详细的二叉树6种遍历方式,以后再补上。

关于这一题,除了中序遍历的方法,还有其他的方法,不过我觉得中序遍历是比较容易理解,时间复杂度O(n)也还可以接受的一种方法。

代码如下:

 class Solution {
public:
vector<int> vec;//用来保存遍历结果
bool isValidBST(TreeNode *root) {
if(!root)
return true; LNR(root); return isSorted(vec);
}
//中序遍历LNR
void LNR(TreeNode *root){
if(root == NULL)
return ;
LNR(root->left);
vec.push_back(root->val);
LNR(root->right);
}
//判断vector是不是升序。
bool isSorted(vector<int> vec){
int len = vec.size();
if(len == )
return true;
for(int i = ; i < len; i++){
if(vec[i] <= vec[i-])
return false;
}
return true;
}
};

【LeetCode练习题】Validate Binary Search Tree的更多相关文章

  1. 【leetcode】Validate Binary Search Tree

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

  2. leetcode dfs Validate Binary Search Tree

    Validate Binary Search Tree Total Accepted: 23828 Total Submissions: 91943My Submissions Given a bin ...

  3. Java for LeetCode 098 Validate Binary Search Tree

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

  4. [LeetCode] 98. Validate Binary Search Tree 验证二叉搜索树

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

  5. Leetcode 98. Validate Binary Search Tree

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

  6. 【leetcode】Validate Binary Search Tree(middle)

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

  7. 【题解】【BST】【Leetcode】Validate Binary Search Tree

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

  8. leetcode 98 Validate Binary Search Tree ----- java

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

  9. [leetcode]98. Validate Binary Search Tree验证二叉搜索树

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

  10. 【leetcode】 Validate Binary Search Tree

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

随机推荐

  1. MIT-scheme安装

    下载地址: http://www.gnu.org/software/mit-scheme/ 下载windows版本,安装. The MIT-Scheme can be installed by jus ...

  2. 百度地图LV1.5实践项目开发工具类bmap.util.jsV1.3

    /** * 百度地图使用工具类-v1.5 * * @author boonya * @date 2013-7-7 * @address Chengdu,Sichuan,China * @email b ...

  3. 网易云课堂_C++开发入门到精通_章节8:设计模式

    课时44设计模式简介 设计模式简介 面向对象设计的第一个原则:针对接口编程,而不是针对实现编程 接口->指针 实现->实例 若已存在一个类Class A,现在希望复用Class A,则有以 ...

  4. python语言磁力搜索引擎源码公开,基于DHT协议

    原文地址: http://www.cnblogs.com/huangxie/p/5550680.html

  5. 【转】android 电池(一):锂电池基本原理篇

    关键词:android  电池关机充电 androidboot.mode charger 平台信息:内核:linux2.6/linux3.0系统:android/android4.0 平台:S5PV3 ...

  6. 【转】C++实现RTMP协议发送H.264编码及AAC编码的音视频

    RTMP(Real Time Messaging Protocol)是专门用来传输音视频数据的流媒体协议,最初由Macromedia 公司创建,后来归Adobe公司所有,是一种私有协议,主要用来联系F ...

  7. MVC4中 jquery validate 不用submit方式验证表单或单个元素

    正确引入MVC4 jquery验证的相关文件 <script src="/Scripts/jquery-1.4.4.js"></script> <sc ...

  8. 事关Animation Tree的工作随笔(一)

    最近的业务上,又回到Animation Tree这块了. 众所周知的是Animation Tree这些概念已经提出很久了,但是使用有着AT支持的CE引擎的项目,却依然义无反顾地没有使用AT,而且,连某 ...

  9. LeetCode OJ平台上Maximum Subarray题目O(n)复杂度解决方式

    原始题目例如以下,意为寻找数组和最大的子串,返回这个最大和就可以. Find the contiguous subarray within an array (containing at least ...

  10. JDBC插入百万数据,不到5秒!

    java自带的批量操作,就可以很好的支持大量数据的处理.相比c#,简单很多.c#要使用oracle提供的ODP.NET,效率才很高,但是代码却很复杂.总之,在这方面,c#没得比.当然,这里的表是没加索 ...