[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 ...
随机推荐
- SpringData
1.什么是SpringData?Spring Data 项目的目的是为了简化构建基于 Spring 框架应用的数据访问计数,包括非关系数据库.Map-Reduce 框架.云数据服务等等:另外也包含对关 ...
- Shiro的Subject和Sessoin的创建
之前要先了解Session的来源Shiro session和Spring session一样吗? 创建Subject的位置 AbstractShiroFilter . doFilterInternal ...
- 用Eclipse进行远程Debug代码
在新的公司,由于项目很大,在本机运行会很慢,所以都是在本地开发,在远程虚拟机上运行.这样就让我痛苦了,我怎么在本地Eclipse上进行debug调试呢,但是在公司前辈的指导下让我知道了本地Eclips ...
- ie8、9 post 跨域
//显示浮层postAjax:function(url,param,callback){ var loadScore = layer_.load(1,{shade: [0.8,'#393D49']}) ...
- openLDAP 2
一.安装OPENLDAP 二.打开安装目录中的文件 slapd.conf 三.安装完成后退出 编辑文本,输入以下内容,并命名为test.ldif dn: dc=company objectClass: ...
- Think In Java 读后感
近期拜读了Think in Java 一书,这里是一些读后感. 此书不仅仅是市面上那种教会你怎么用系统API来编程的书,那种书太多. 此书不仅仅从头开始讲述了如何 ...
- java 多线程下载文件 以及URLConnection和HttpURLConnection的区别
使用 HttpURLConnection 实现多线程下载文件 注意GET大写//http public class MultiThreadDownload { public static void m ...
- Opencv读取图片像素值
#include <iostream>#include <opencv2/opencv.hpp> using namespace std;using namespace cv; ...
- table 合并行和列
table合并行列,以及拆分 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "htt ...
- Python 中 "is" 与 "==" 操作有什么区别?
转自:https://foofish.net/what-is-difference-between-is-and-euqals.html 在 Python 中,比较两个对象(变量)是否相等,可以用 & ...