【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 ...
随机推荐
- "Java 反序列化"过程远程命令执行漏洞
一.漏洞描述 国外 FoxGlove 安全研究团队于2015年11月06日在其博客上公开了一篇关于常见 Java 应用如何利用反序列化操作进行远程命令执行的文章.原博文所提到的 Java 应用都使 ...
- Java Lambda表达式入门
Java Lambda表达式入门 http://blog.csdn.net/renfufei/article/details/24600507 Java 8十个lambda表达式案例 http://w ...
- 基本linux命令
1.mkdir mkdir 创建目录 mkdir -p 循环创建目录 2.cd 切换目录 3.pwd 查看当前路径 4.mkdir 删除一个空的目录 5.cp 复制文件/目录 -r用 ...
- Object&&String学习
Object类 列表项 String类 常用方法 构造方法 public String() public String(byte[] bytes) public String(byte[]bytes, ...
- sqoop
http://blog.csdn.net/yfkiss/article/details/8700480 http://www.cnblogs.com/admln/p/sqoop1-99-4-javaa ...
- WCF :IIS寄宿方式的Web地址、BaseAddress和EndPoint Address的关系
对于在IIS中通过W3SVC或WAS寄宿的WCF Service,其在浏览器中显示的地址(Web地址),与其配置文件中的BaseAddress和EndPoint Address有什么关系呢?让我们来分 ...
- IBM:领导力素质的三环模式
文章来源:http://blog.vsharing.com/sdtcj/A1826934.html 在翰威特公司于2003年评选的美国企业“领导人才最佳雇主”名单上,IBM名列榜首.而纽约< ...
- UESTC 1852 Traveling Cellsperson
找规律水题... Traveling Cellsperson Time Limit: 1000ms Memory Limit: 65535KB This problem will be judged ...
- Spring配置bean文件的底层实现方式
首先 bean文件如下: <beans> <bean id="date" class="java.util.Date"></bea ...
- Linux 信号量大全
编号 信号名称 缺省动作 说明 1 SIGHUP 终止 终止控制终端或进程 2 SIGINT 终止 键盘产生的中断(Ctrl-C) 3 SIGQUIT dump 键盘产生的退出 4 SIGILL du ...