【Validate Binary Search Tree】cpp
题目:
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.
代码:
/**
* 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) return true;
stack<TreeNode *> sta;
vector<int> traversal;
TreeNode *p = root;
while ( !sta.empty() || p )
{
if (p){
sta.push(p);
p = p->left;
}
else{
p = sta.top();
sta.pop();
if ( !traversal.empty() && traversal[traversal.size()-]>=p->val ) return false;
traversal.push_back(p->val);
p = p->right;
}
}
return true;
}
};
tips:
中序遍历BST;并保留每次访问的元素;如果中序遍历的前一个元素不小于后一个元素,则返回false;全部遍历过后无问题,则返回true。
题目中有一个test case是:[-2147483648],即INT_MIN。由于有这个非常极端的case存在,之前那种设一个pre = INT_MIN的办法就行不通了,暂时老老实实用一个vector来代替。
==========================================
一开始刷这道题总想用递归,后来看的之前的代码,BST这种判断有效的,用中序遍历可能更好一些。
/**
* 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) {
stack<TreeNode*> sta;
TreeNode* curr = root;
vector<int> vals;
while ( !sta.empty() || curr )
{
if ( curr )
{
sta.push(curr);
curr = curr->left;
}
else
{
curr = sta.top();
vals.push_back(curr->val);
sta.pop();
if ( vals.size()> && vals[vals.size()-]<=vals[vals.size()-] ) return false;
curr = curr->right;
}
}
return true;
}
};
【Validate Binary Search Tree】cpp的更多相关文章
- 【二叉查找树】03验证是否为二叉查找树【Validate Binary Search Tree】
本质上是递归遍历左右后在与根节点做判断,本质上是后序遍历 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ...
- 【Recover Binary Search Tree】cpp
题目: Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without chan ...
- 【Lowest Common Ancestor of a Binary Search Tree】cpp
题目: Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in th ...
- 【遍历二叉树】07恢复二叉搜索树【Recover Binary Search Tree】
开一个指针数组,中序遍历这个二叉搜索树,将节点的指针依次保存在数组里, 然后寻找两处逆序的位置, 中序便利里BST得到的是升序序列 ++++++++++++++++++++++++++++++++++ ...
- 【Unique Binary Search Trees】cpp
题目: Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For e ...
- 【Convert Sorted List to Binary Search Tree】cpp
题目: Given a singly linked list where elements are sorted in ascending order, convert it to a height ...
- 【Convert Sorted Array to Binary Search Tree】cpp
题目: Given an array where elements are sorted in ascending order, convert it to a height balanced BST ...
- 【leetcode】Validate Binary Search Tree
Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...
- 【LeetCode练习题】Validate Binary Search Tree
Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...
随机推荐
- DPDK内存管理-----(二)rte_mempool内存管理
DPDK以两种方式对外提供内存管理方法,一个是rte_mempool,主要用于网卡数据包的收发:一个是rte_malloc,主要为应用程序提供内存使用接口.本文讨论rte_mempool.rte_me ...
- css3 2d
CSS3 2D 转换 通过 CSS3 转换,我们能够对元素进行移动.缩放.转动.拉长或拉伸. 以下是 2D 转换 1 translate()通过 translate() 方法,元素从其当前位置移动 ...
- python restful 框架之 eve 外网访问设置
官网地址: http://python-eve.org/ 配合mongodb进行crud使用起来很方便,但是部署的时候遇到一个问题,按照官网和Deom说的,servername使用 '127.0.0. ...
- asp 301跳转代码
<% Response.Status="301 Moved Permanently" Response.AddHeader "Location&quo ...
- linux基本使用(一)
分区1./ 根分区2. swap 交换分区(大小建议是内存的1~2倍)3. /home 分区4./boot 引导文件(启动加载)分区5./var 等,最低 要有前2个分区吧,最好有home分区,因为没 ...
- PHP变量作用域以及地址引用问题
作用域的概念: 在PHP脚本的任何位置都可以声明变量,但是,声明变量的位置会大大影响访问变量的范围.这个可以访问的范围称为作用域. 主要的常用的包括:局部变量.全局变量.静态变量. 1.局部变量:就是 ...
- PHP实例 表单数据插入数据库及数据提取 用户注册验证
网站在进行新用户注册时,都会将用户的注册信息存入数据库中,需要的时候再进行提取.今天写了一个简单的实例. 主要完成以下几点功能: (1)用户进行注册,实现密码重复确认,验证码校对功能. (2)注册成功 ...
- ASP.NET实现弹出框真分页将复选框选择的数据存到数据库中(四)
这是第四步点击保存将信息存入数据库中. 这个就简单了利用ajax将JSON字符串传到后台然后这里有个知识点就是将DataTable直接存入数据库中.代码如下: 一.界面获取数据JS代码: //保存订单 ...
- ThinkPHP 3.2.2跨控制器调用方法
所谓跨控制器调用,指的是在一个控制器中调用另一个控制器的某个方法.在ThinkPHP中有三种方式实现跨控制器调用: 直接实例化: A()函数实例化; R()函数实例化. (1)直接实例化 直接实例 ...
- 微信支付开发,再次签名,APP调用
1.商户服务器生成支付订单,先调用[统一下单API]生成预付单,获取到prepay_id后将参数再次签名传输给APP发起支付. 再次生成签名的时候,按照接口: https://pay.weixin.q ...