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. 贝塞尔曲线:原理、自定义贝塞尔曲线View、使用!!!

    一.原理 转自:http://www.2cto.com/kf/201401/275838.html Android动画学习Demo(3) 沿着贝塞尔曲线移动的Property Animation Pr ...

  2. Jquery EasyUI修改行背景的两种方式

    1.数据加载完成不请求后台的做法 方式一: //更改表格行背景 function changeLineStyle(index){ var rows=$("#alertGird"). ...

  3. .net项目中上传大图片失败

    .net项目中有时用户提出要上传大图片,一张图片有可能十几兆,本来用的第三方的上传控件,有限制图片上传大小的设置,以前设置的是2M.按照用户的要求,以为直接将限制图片上传大小的设置改下就可以了,但是当 ...

  4. Ubuntu+Eclipse+ADT+Genymotion+VirtualBox开发环境搭建

    1.Eclispe安装就不说了 2.以下说说怎样安装ADT插件.有两种途径: (1)在线安装: 地址:https://dl-ssl.google.com/android/eclipse/(只是近期天朝 ...

  5. C++ 採集音频流(PCM裸流)实现录音功能

    与上一篇的"C++ 播放音频流(PCM裸流)" 点击打开链接 相相应,本篇是关于用C++实现录音功能的.相同是直接建一个win32控制台程序然后将代码拷过去改个文件名称就能够用,也 ...

  6. 一、MP3文件概述

    一.概述 MP3 文件是由帧(frame)构成的,帧是 MP3 文件最小的组成单位.MP3 的全称应为 MPEG1 Layer-3 音频文件,MPEG(Moving Picture Experts G ...

  7. transition的唧唧歪歪

    transition是css3新出的一个属性,大白话叫做过渡. 主要有下面这四个属性: transition-property.transition-duration.transition-timin ...

  8. Model注解的后台原理

    Asp.net MVC的验证特性是由模型绑定器.模型元数据.模型验证器和模型状态组成的协调系统的一部分. 1.验证和模型绑定 默认情况下,Asp.net MVC框架在模型绑定石执行验证逻辑,在操作方法 ...

  9. SQL日期格式转换(经常用又经常忘记的东西)转载自http://www.cnblogs.com/wangyuelang0526/archive/2012/06/06/2538224.html

    Select CONVERT(varchar(100), GETDATE(), 8):14:53:14Select CONVERT(varchar(100), GETDATE(), 9): 06 6 ...

  10. hdu3123GCC

    Problem Description The GNU Compiler Collection (usually shortened to GCC) is a compiler system prod ...