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的更多相关文章

  1. [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 ...

  2. [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 ...

  3. [LeetCode] Recover Binary Search Tree 复原二叉搜索树

    Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...

  4. [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 ...

  5. LeetCode Closest Binary Search Tree Value II

    原题链接在这里:https://leetcode.com/problems/closest-binary-search-tree-value-ii/ 题目: Given a non-empty bin ...

  6. LeetCode Closest Binary Search Tree Value

    原题链接在这里:https://leetcode.com/problems/closest-binary-search-tree-value/ Given a non-empty binary sea ...

  7. leetcode@ [173] Binary Search Tree Iterator (InOrder traversal)

    https://leetcode.com/problems/binary-search-tree-iterator/ Implement an iterator over a binary searc ...

  8. [leetcode]Recover Binary Search Tree @ Python

    原题地址:https://oj.leetcode.com/problems/recover-binary-search-tree/ 题意: Two elements of a binary searc ...

  9. ✡ leetcode 173. Binary Search Tree Iterator 设计迭代器(搜索树)--------- java

    Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...

随机推荐

  1. ORA-15005: name "orcl" is already used by an existing alias

    在进行ASM操作的时候,如果目录不存在的话,那么可能会报如下的错误: <pre name="code" class="plain">RMAN> ...

  2. 【和我一起学python吧】python的数据类型

    python的元组.列表.字典数据类型是很python(there python is a adjective )的数据结构.这些结构都是经过足够优化后的,所以如果使用好的话,在某些area会有很大的 ...

  3. 在window server 2008 64位系统上 发布网站的过程中遇到的问题

    发布网站的过程如下: 1.安装数据库系统2.建立数据库,执行sql3.安装iis4.在本地机子上发布网站5.把发布好的东西拷贝到IIS上 1.安装数据库系统: 出现错误:必须使用角色管理工具 安装或配 ...

  4. data audit on hadoop fs

    最近项目中遇到了存储在HDFS上的数据格式不对,是由于数据中带有\r\n的字符,程序处理的时候没有考虑到这些情况.历史数据大概有一年的时间,需要把错误的数据或者重复的数据给删除了,保留正确的数据,项目 ...

  5. 解决问题的步骤(第一篇)-- clwu

    现象: 之前打开IE 还是正常的,但前几天开始打开就不正常了,报错如下. 处理(别人的)问题的步骤: 百度一下  0xc0000018,没有什么有用信息. 看一下程序(IE)启动时做了些什么. 怎么看 ...

  6. Dapper基础用法

    假如你喜欢原生的Sql语句,又喜欢ORM的简单,那你一定会喜欢上Dapper这款ROM.点击下载Dapper的优势:1,Dapper是一个轻型的ORM类.代码就一个SqlMapper.cs文件,编译后 ...

  7. 设置sonar 界面为中文环境

    sonar 默认是英文的界面 1.下载http://repository.codehaus.org/org/codehaus/sonar-plugins/l10n/sonar-l10n-zh-plug ...

  8. 【转】Nginx系列(五)--nginx+tomcat实现负载均衡

    原博文出于:  http://blog.csdn.net/liutengteng130/article/details/47129909   感谢! Nginx占有内存少,并发能力强,事实上Nginx ...

  9. fx-experience-tools

    http://fxexperience.com/2012/03/announcing-fx-experience-tools/ I have some cool new stuff for you t ...

  10. HTML5每日一练之input新增加的URL类型与email类型应用

    1.URL类型: <form> <input name="urls" type="url" value="http://www.w3 ...