LeetCode: Validata Binary Search Tree
LeetCode: Validata 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.
地址:https://oj.leetcode.com/problems/validate-binary-search-tree/
算法:中序遍历,看看遍历到的节点值是否递增。代码:
/**
* 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) return true;
stack<TreeNode*> stk;
TreeNode *p = root;
while(p){
stk.push(p);
p = p->left;
}
TreeNode *pre = NULL;
p = NULL;
while(!stk.empty()){
pre = p;
p = stk.top();
stk.pop();
if(pre && p->val <= pre->val){
return false;
}
if(p->right){
TreeNode *q = p->right;
while(q){
stk.push(q);
q = q->left;
}
}
}
return true;
}
};
LeetCode: Validata Binary Search Tree的更多相关文章
- [LeetCode] Closest Binary Search Tree Value II 最近的二分搜索树的值之二
Given a non-empty binary search tree and a target value, find k values in the BST that are closest t ...
- [LeetCode] Closest Binary Search Tree Value 最近的二分搜索树的值
Given a non-empty binary search tree and a target value, find the value in the BST that is closest t ...
- [LeetCode] Recover Binary Search Tree 复原二叉搜索树
Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...
- [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 Closest Binary Search Tree Value II
原题链接在这里:https://leetcode.com/problems/closest-binary-search-tree-value-ii/ 题目: Given a non-empty bin ...
- LeetCode Closest Binary Search Tree Value
原题链接在这里:https://leetcode.com/problems/closest-binary-search-tree-value/ Given a non-empty binary sea ...
- leetcode@ [173] Binary Search Tree Iterator (InOrder traversal)
https://leetcode.com/problems/binary-search-tree-iterator/ Implement an iterator over a binary searc ...
- [leetcode]Recover Binary Search Tree @ Python
原题地址:https://oj.leetcode.com/problems/recover-binary-search-tree/ 题意: Two elements of a binary searc ...
- ✡ leetcode 173. Binary Search Tree Iterator 设计迭代器(搜索树)--------- java
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...
随机推荐
- Unity3d 基于物理渲染Physically-Based Rendering之specular BRDF
在实时渲染中Physically-Based Rendering(PBR)中文为基于物理的渲染它能为渲染的物体带来更真实的效果,而且能量守恒 稍微解释一下字母的意思,为对后文的理解有帮助,从右到左L为 ...
- 利用Spring.Net技术打造可切换的分布式缓存读写类
利用Spring.Net技术打造可切换的Memcached分布式缓存读写类 Memcached是一个高性能的分布式内存对象缓存系统,因为工作在内存,读写速率比数据库高的不是一般的多,和Radis一样具 ...
- (转载)OC学习篇之---类的三大特性:封装,继承,多态
之前的一片文章介绍了OC中类的初始化方法和点语法的使用,今天来继续学习OC中的类的三大特性,我们在学习Java的时候都知道,类有三大特性:继承,封装,多态,这个也是介绍类的时候,必须提到的话题,那么今 ...
- 在Ubuntu6.06 在搭建SVN服务器及在windows建立svn+ssh客户端 (续)
接上篇.本篇主要介绍windows下建立svn+ssh客户端. 9.在windows下安装svn客户端,则需安装“TortoiseSVN”.“Puttygen”和“Pageant” 9.1.下载 ...
- Tkinter教程之Event篇(2)
本文转载自:http://blog.csdn.net/jcodeer/article/details/1823548 '''Tkinter教程之Event篇(2)''''''5.测试离开(Leave) ...
- [转] Web前端优化之 Cookie篇
原文链接: http://lunax.info/archives/3095.html Web 前端优化最佳实践第三部分面向 Cookie .目前只有 2 条实践规则. 1. 缩小 Cookie (Re ...
- Cloudera CDH5 部署实战指南(离线安装)
配置软件源服务器 1.安装createreporpm -ivh deltarpm-3.5-0.5.20090913git.el6.x86_64.rpm rpm -ivh python-deltarpm ...
- http协议中的Content-Type
今天对http协议中的Content-Type有所理解了 它的主要功给我的感觉,还是在前台(客户端)给服务器传输数据时,描述这个数据的格式. 比如,我只传一个表单数据,但这个表单中只有文本,没有其它的 ...
- Varnish – 高性能http加速器
Varnish是一款高性能且开源的反向代理服务器和http加速器.与传统的Squid相比,Varnish具有性能更高.速度更快.管理更方便 等诸多优点.作者Poul-Henning Kamp是Free ...
- HDU 5794 A Simple Chess (容斥+DP+Lucas)
A Simple Chess 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5794 Description There is a n×m board ...