【LeetCode练习题】Validate Binary Search Tree
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的更多相关文章
- 【leetcode】Validate Binary Search Tree
Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...
- leetcode dfs Validate Binary Search Tree
Validate Binary Search Tree Total Accepted: 23828 Total Submissions: 91943My Submissions Given a bin ...
- 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 ...
- [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 ...
- 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 ...
- 【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 ...
- 【题解】【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 ...
- 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 ...
- [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 ...
- 【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 ...
随机推荐
- js 环形链表
function link($no){ this.no = $no; this.next;}function addLink($num){ var $first=$cur = {} ...
- Windows7安装SQL Server 2008图解
不知是什么时候,把这篇博客给删除了,今天才发现,想恢复好像又不行,所以重新发布一下吧! 这几天因为需要,一直想安装SQL Server 2008来作为Web后台的数据库进行些实验,但总是没有时间,今天 ...
- logstash grok 解析Nginx
log_format main '$remote_addr [$time_local] "$request" ' '$request_body $status $body_byte ...
- 了解Serialization
对象的串行化(Serialization) 一.串行化的概念和目的 1.什么是串行化 对象的寿命通常随着生成该对象的程序的终止而终止.有时候,可能需要将对象的状态保存下来,在需要时再将对象恢复.我们把 ...
- index < m_IntCount错误
在inspector中,重新添加动画的过度条件即可. 参考
- Oracle SQL函数之转换函数To_char汇总
TO_CHAR(x[[,c2],C3])[功能]将日期或数据转换为char数据类型[参数]x是一个date或number数据类型.c2为格式参数c3为NLS设置参数如果x为日期nlsparm=NLS_ ...
- js打印数据类型
console.log({}.toString.call(123))--- [object Number].... [object String] [object Undefined] [object ...
- 浅谈postMessage多页面监听事件
最近做了一个Echarts和Highcharts多图多页面连动的效果,就用到postMessage 如下介绍: 最开始在最外围的页面也就是所有页面的父级页面添加postMessage监听事件以便监听下 ...
- 转:C#整数三种强制类型转换int、Convert.ToInt32()、int.Parse()的区别
1.int适合简单数据类型之间的转换,C#的默认整型是int32(不支持bool型); 2.int.Parse(string sParameter)是个构造函数,参数类型只支持string类型; 3. ...
- VS2003.NET在文件中查找卡死
不知怎么的,安装vs2003后,一点查找就卡死. 修复方法:修改devenv.exe的兼容性配置,勾选“禁用视觉主题”! 说实话,还真不知道这两者有什么关系?