【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 ...
随机推荐
- Android高德地图开发具体解释
这段时间开发的时候用到了高德地图,对高德地图开发有心得体会,如今分享给大家.对我开发过百度地图的我来说,整体来说高德地图Demo,没有百度解说的具体 个人更偏向于使用百度地图,可是没办发,项目须要使用 ...
- java实现xml-rpc客户端和服务端
客户端代码: package test_xmlrpc.test; import java.net.URL;import java.util.ArrayList;import java.util.Lis ...
- 安装配置MongoDB数据库
一.关闭SElinux.配置防火墙 1.vi /etc/selinux/config #SELINUX=enforcing #注释掉 #SELINUXTYPE=targeted #注释掉 SELINU ...
- Struts2(三)——数据在框架中的数据流转问题
一款软件,无在乎对数据的处理.而B/S软件,一般都是用户通过浏览器客户端输入数据,传递到服务器,服务器进行相关处理,然后返回到指定的页面,进行相关显示,完成相关功能.这篇博客重点简述一下Struts2 ...
- JNI_最简单的Java调用C/C++代码
JNI_最简单的Java调用C/C++代码 JNI.是Java Native Interface的简称,中文是"Java本地调用".通过这种技术能够做到下面两点: Java程序中的 ...
- NET基础课--组件2
强命名组件:使用sn.exe生成公钥私钥对,公钥可以用工具查看.snk文件需严格保护. sn -k d:\iron.snk 生成公钥私钥对 sn -p d:\iron.snk d:\ ...
- Http 信息头
Http协议定义了很多与服务器交互的方法,最基本的有4种,分别是GET.POST.PUT.DELETE.一个URL地址用于描述一个网络上的资源,而HTTP中的GET.POST.PUT. DELETE就 ...
- su普通用户切换root用户失败
http://blog.itpub.net/26432034/viewspace-1688391/ http://blog.csdn.net/zhangdaiscott/article/details ...
- setlocal enabledelayedexpansion
http://www.jb51.net/article/29323.htm 例1: 代码如下: @echo off set a=4 set a=5&echo %a% pause 结果:4 解 ...
- 记微信开发(自定义回复&关注回复)
记微信开发(自定义回复&关注回复) 记微信开发(自定义回复&关注回复) code: <?php/** * wechat php test *///define your toke ...