【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 ...
随机推荐
- jqxTreeGrid
基本TreeGrid样本 <!DOCTYPE html> <html lang="en"> <head> <title id=" ...
- [Angularjs]表单验证
写在前面 在开发中提交表单,并对表单的值进行验证是非常常见的操作,angularjs对表单验证提供了非常好的支持. demo 表单 <form name="myform" n ...
- 给各位聚聚和大大介绍一个开源项目 Expression2Sql(转)
阅读目录 一.Expression2Sql介绍 二.单表简单查询 三.Where条件 四.多表关联查询 五.group by 六.order by 七.函数 八.delete 删除 九.update ...
- Vim 的 tab 设置
文章转自:http://blog.csdn.net/shell_picker/article/details/6033023 摘自 Vim 手册: 选项:1. tabstop:表示一个 tab 显示出 ...
- http request method and response codes
============================ HTTP_Method============================ HTTP Method Action Examples GET ...
- godaddy空间的sql server数据库没办法insert中文
原来的代码: use string007 from sysobjects where id = object_id('K_V_TEST') and type = 'U') drop table K_V ...
- C#4.0 特性
动态绑定 命名和可选参数 泛型的协变和逆变 互操作性 动态支持 Office 可编程性 类型等效性支持 协变和逆变 当类型为dynamic的视图模型遭遇匿名对象 https://msdn.micros ...
- 使用type="redirect"重定向,传递List等变量到jsp页面的问题
Struts2在提交表单的时候,使用「type="redirect"」重定向到相应的jsp页面. Action中的List表单是无法传到相应的jsp页面. 我猜测是因为List作为 ...
- POJ 3744 Scout YYF I
分段的概率DP+矩阵快速幂 Scout YYF I Time Limit: 1000MS Memory Limit: 65536K Total Sub ...
- mongodb python image 图像存储读取
最近做一些数据库调研的工作,目标是实现影像更快的入库.出库.查询,并实现并行访问等操作. 将结果总结成一个mongoImg类,也算是小结吧. ''' Created on 2013-8-6 class ...