[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 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?
The serialization of a binary tree follows a level order traversal, where '#' signifies a path terminator where no node exists below.
Here's an example:
1
/ \
2 3
/
4
\
5
The above binary tree is serialized as "{1,2,3,#,#,4,#,#,5}".
class Solution {
public:
bool isValidBST(TreeNode *root) {
if(!root ||(!root->left && !root->right)) return true;
vector<int> vi;
vi.clear();
// 用非递归的方式对树进行中序遍历,将结果存放到vi数组中
stack<TreeNode* > s;
TreeNode *tmp=root;
while(!s.empty() || tmp){
if(tmp){
s.push(tmp);
tmp = tmp->left;
}else{
tmp = s.top();
s.pop();
vi.push_back(tmp->val);
tmp = tmp->right;
}
}
//对中序遍历的结果进行判断,注意不能有重复的数字
for(int i=;i<vi.size();i++){
if(vi[i]<=vi[i-]) return false;
}
return true;
}
};
转载请注明出处: http://www.cnblogs.com/double-win/ 谢谢!
[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 ...
- 【题解】【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 ...
- 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 ...
- 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 ...
随机推荐
- leetcode674
public class Solution { public int FindLengthOfLCIS(int[] nums) { var len = nums.Length; ) { return ...
- autofs文件自动挂载系统
为了自己使用方便,利用iso文件制作了自己的rhel6.4的yum源,每次都手动进行mount,十分不便 后来在/etc/rc.local中写入了mount命令,方便多了. 时间一久,发现新的问题出现 ...
- 【译】PGS字幕
PGS(Presentation graphic stream):图形字幕流,是用来显示蓝光电影中的字幕的流.当蓝光盘中的PGS格式的字幕被分离存储的时候通常保存在一个以sup为扩展名的文件中.(也可 ...
- 引用rtmp编译报错:rtmp.obj : error LNK2001: 无法解析的外部符号 __imp__timeGetTime@0
如题vs下引用librtmp的时候报错:rtmp.obj : error LNK2001: 无法解析的外部符号 __imp__timeGetTime@0 在link 里加入 winmm.lib 就可以 ...
- WOW研究资料收集
1,模拟服务器:trinity core, sunwell core等 参考:逍遥魔兽 2,大芒果:通用网游框架,带了WOW的模拟模块 参考资料: 大芒果论坛http://www.mangoscn.c ...
- Keepalived 资源监控
简介: 作为一个高可用集群软件,Keepalived 没有 Heartbeat .RHCS 等专业的高可用集群软件功能强大,它不能够实现集群资源的托管,也不能实现对集群中运行服务的监控,好在 Keep ...
- Zabbix 监控 Mysql 状态
简介: 如何使用 Zabbix 来监控 Mysql 状态 ? Zabbix 有自带监控 Mysql 的模板,但是却不能直接使用.. 需要我们根据模板提供的 Key 自己写脚本获取数据 1.查看都有哪些 ...
- SPI子系统分析之一:框架
内核版本:3.9.5 SPI子系统概述: 一个SPI主控制器对应一条SPI总线,当然在系统中有唯一的总线编号. SPI总线上有两类设备: 其一是主控端,通常作为SOC系统的一个子模块出现,很多嵌入式M ...
- Unity 之 Shader 面的剔除 Cull
面的剔除 Cull 在渲染的时候,默认情况下是只有朝向摄像机的面才会被渲染,可以告诉Unity,我想渲染哪一个朝向的面,使用Cull命令在计算体积阴影的时候会用到对Cull的操作来计算和物体相交的投影 ...
- 一些好用的 Oracle 批处理和语句
# 备份脚本 backup.bat @ECHO OFF COLOR 0A SET DaysAgo=1 SET Today=%date:~0,4%%date:~5,2%%date:~8,2% EXP u ...