【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 trees.
confused what "{1,#,2,3}" means? > read more on how binary tree is serialized on OJ.
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool isValidBST(TreeNode *root) { if(root==NULL)
{
return true;
} if(root->left==NULL&&root->right==NULL)
{
return true;
} //注意如果测试用例含有INT_MAX则,必须采用long long int 才能避免出错
return testValid(root,(long long int)INT_MAX+,(long long int)INT_MIN-);
} bool testValid(TreeNode *node,long long int max, long long int min)
{
if(node==NULL)
{
return true;
} return node->val<max&&node->val>min&&testValid(node->left,node->val,min)&&testValid(node->right,max,node->val); }
};
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<int> ret_v;
bool isValidBST(TreeNode *root) { if(root==NULL)
{
return true;
} ret_v.clear();
inOrderTraversal(root); //判断是否递增
for(int i=;i<ret_v.size()-;i++)
{
if(ret_v[i]>=ret_v[i+])
{
return false;
}
}
return true;
} //中序遍历,记录下数值
void inOrderTraversal(TreeNode *root)
{
if(root==NULL)
{
return;
} inOrderTraversal(root->left);
ret_v.push_back(root->val);
inOrderTraversal(root->right);
}
};
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public: int ret;
bool flag;
bool isFirstNode; bool isValidBST(TreeNode *root) { flag=true;
//判断是不是第一个被访问的节点
isFirstNode=true; inOrderTraversal(root);
return flag;
} void inOrderTraversal(TreeNode *root)
{
if(root==NULL)
{
return;
} if(!flag)
{
return;
} inOrderTraversal(root->left); //一旦发现不符合升序,则不是二叉排序树
if(!isFirstNode&&ret>=root->val)
{
flag=false;
return;
} ret=root->val;
isFirstNode=false; inOrderTraversal(root->right);
} };
【leetcode】Validate Binary Search Tree的更多相关文章
- 【LeetCode】Validate Binary Search Tree ——合法二叉树
[题目] Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defin ...
- 【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】 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). 知识点:BST的特点: 1.一个节点的左子树 ...
- 【Leetcode】【Medium】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】二叉查找树 binary search tree(共14题)
链接:https://leetcode.com/tag/binary-search-tree/ [220]Contains Duplicate III (2019年4月20日) (好题) Given ...
- 【LeetCode】173. Binary Search Tree Iterator 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 保存全部节点 只保留左节点 日期 题目地址:http ...
- 【leetcode】Recover Binary Search Tree
Recover Binary Search Tree Two elements of a binary search tree (BST) are swapped by mistake. Recove ...
随机推荐
- thinkphp 3.2.3 连接sql server 2014 WAMPSERVER环境包
安装 sqlsrv 扩展 首先 sql server 2014 安装没啥说的 链接信息自己设置 php 版本 :5.5.12 sqlsrv 驱动 微软提供了 3.0 和3.1 版本 3.0 对应 ...
- CentOS下nginx简单安装
说明:环境 系统:Centos 6 软件包:nginx-1.2.4 配置系统yum源 #/etc/yum.repos.d/ #rm -rf ./* vi localhost.repos.d [yumy ...
- Java多线程编程核心技术---对象及变量的并发访问(二)
数据类型String的常量池特性 在JVM中具有String常量池缓存的功能. public class Service { public static void print(String str){ ...
- apt-get 与 yum的区别 (转)
一般来说著名的linux系统基本上分两大类:1.RedHat系列:Redhat.Centos.Fedora等2.Debian系列:Debian.Ubuntu等 RedHat 系列 1 常见的安装包格式 ...
- 调整Linux磁盘分区的大小的方法
昨天数据入库时,一直报错,说磁盘满了,,df -h 一看,发现/目录下只有50G空间,已使用49G:我的程序和dbss都安装在/目录下,ftp到的数据放在/data下的一个子目录下,分解完的 ...
- 完全迁移到red hat来的相关问题解决和配置
默认从光盘iso镜像安装iso-1 时, yum.repos.d只有 packagekit-media.repo, 要从网上下载一个 CentOS-Base.repo文件放到这里. redhat上下载 ...
- unity资源管理
Resources.Load(path); 每次执行都会真的去从硬盘加载资源,如果不希望这样做,那就保存第一次返回的引用,下次直接使用即可. Resources.UnloadAsset(obj); 该 ...
- 成功的背后!(给所有IT人)
转载:来自CSDN第一名博主:http://blog.csdn.net/phphot/article/details/2187505 成功的背后,有着许多不为人知的故事,而正是这些夹杂着泪水和汗水的过 ...
- [译]git rebase -i
使用rebase -i会在终端出现一个交互页面. 在这个交互页面中我们可以对要rebase的commit做一定的修改. 用法 git rebase -i <master> 把当前的分支的c ...
- input lable水平对齐
1.CSS <style type="text/css"> input,label { vertical-align:middle;} </style ...