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.
Example 1:
2
/ \
1 3
Binary tree [2,1,3]
, return true.
Example 2:
1
/ \
2 3
Binary tree [1,2,3]
, return false.
分析: 判断中序遍历的是不是逐渐增大的,如果不是,return false, 如果是,return true。
/**
* Definition for a binary tree node.
* 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;
stack<TreeNode*> s;
TreeNode* cur=root;
TreeNode* pre=nullptr;
while(){ while(cur){
s.push(cur);
cur =cur->left;
}
if(s.empty())
break;
cur = s.top();
s.pop();
if(pre!=nullptr && pre->val >= cur->val)
return false;
pre = cur;
cur = cur->right;
} return true;
}
};
Validate Binary Search Tree的更多相关文章
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
- 【leetcode】Validate Binary Search Tree
Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...
- LintCode Validate Binary Search Tree
Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...
- 39. Recover Binary Search Tree && Validate Binary Search Tree
Recover Binary Search Tree OJ: https://oj.leetcode.com/problems/recover-binary-search-tree/ Two elem ...
- [CareerCup] 4.5 Validate Binary Search Tree 验证二叉搜索树
4.5 Implement a function to check if a binary tree is a binary search tree. LeetCode上的原题,请参见我之前的博客Va ...
- 【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 ...
- LeetCode: Validate Binary Search Tree 解题报告
Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...
- 【LeetCode】98. Validate Binary Search Tree (2 solutions)
Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...
随机推荐
- GHOST WIN7系统64位经典优化版 V2016年
来自系统妈:http://www.xitongma.com 深度技术GHOST win7系统32,64位经典优化版 V2016年3月 系统概述 深度技术ghost win7系统64位经典优化版适用于笔 ...
- [在线] html 转 pdf
http://www.htm2pdf.co.uk/
- git之三
1.status命令与diff命令 前面我们已经成功地添加并提交了一个readme.txt文件,修改readme.txt如下: echo "Git is a distributed vers ...
- DataTable操作
一 复制DataTable中符合条件的DataRow到新的DataTable中 One: DataTable TableTemp = new DataTable();//临时table DataTab ...
- STM32时钟理解
转载自 http://blog.sina.com.cn/s/blog_6ebd49350100q6xw.html STM32时钟理解 一.硬件上的连接问题 如果使用内部RC振荡器而不使用外部晶振,请按 ...
- [转载]利用@media screen实现网页布局的自适应,@media screen and
开始研究响应式web设计,CSS3 Media Queries是入门.Media Queries,其作用就是允许添加表达式用以确定媒体的环境情况,以此来应用不同的样式表.换句话说,其允许我们在不改变内 ...
- dba诊断之IO
--查看占用系统io较大的session SELECT se.sid,se.serial#,pr.SPID,se.username,se.status,se.terminal,se.program ...
- Linux安装Mysql rpm
1.安装时候会提示与已经安装的RPM包有冲突,所以我们先卸载一些RPM包,要卸载哪些呢?我们要卸载的是包含有mariadb关键字的RPM包,执行命令:rpm -qa|grep mariadb来查看一下 ...
- KVM 介绍(5):libvirt 介绍 [ Libvrit for KVM/QEMU ]
学习 KVM 的系列文章: (1)介绍和安装 (2)CPU 和 内存虚拟化 (3)I/O QEMU 全虚拟化和准虚拟化(Para-virtulizaiton) (4)I/O PCI/PCIe设备直接分 ...
- 睡觉问题早晚成为我顶头疼的问题。。。-PHP
hi 昨晚又作自己,睡不好整个人都不好... 1.PHP实现页面静态化 二.纯静态化 2.2 实现页面纯静态化的原理 --基本方式 file_put_contents()函数: 使用php内置缓存机制 ...