【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) ...
随机推荐
- view上添加点手势 button无法响应点击事件
在view 上添加手势 有的时候 会把Button的 点击事件盖掉,这个 时候 我们用UITapGestureRecognizer的代理方法 //手势的代理方法 - (BOOL)gestureRec ...
- 开发iOS应用程序需要的工具和编程技术
似乎每个iOS应用开发学习者都是从这篇文章开始写博客的,我也不例外,也从这里写吧,内容雷同,但绝对是原创.因为一直相信,通过自己的理解写出来,是掌握技术一个很好的途径. Xcode苹果最为优秀的集成开 ...
- 利用Python抓取CSDN博客
这两天发现了一篇好文章,陈皓写的makefile的教程,具体地址在这里<跟我一起写makefile> 这篇文章一共分成了14个部分,我看东西又习惯在kindle上面看,感觉一篇一篇地复制成 ...
- Winform 打开下载的文件
private void OpenFile(string filename) { ProcessStartInfo sInfo = new ProcessStartInfo(); sInfo.Wind ...
- C# Async与Await用法
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- HTTP协议的URI及各种方法
每个Web服务器资源都有一个名字,这样客户端就可以说明他们感兴趣的资源是什么了,服务器资 源名被统称为:统一资源标识符(Uniform Resource Identifier, URI) Joe的五金 ...
- scala实现Netty通信
在学习spark源码的时候看到spark在1.6之后底层的通信框架变成了akka和netty两种方式,默认的是用netty根据源码的思路用scala写了一个Demo级别的netty通信 package ...
- c语言结构体保存并输出学生信息
最近在学习数据结构,巩固下c语言. #include<stdio.h> /*定义结构体student并设置别名stud*/ /*typedef struct student{ int nu ...
- C# Sql 触发器
触发器是一种特殊类型的存储过程,它不同于之前的我们介绍的存储过程.触发器主要是通过事件进行触发被自动调用执行的.而存储过程可以通过存储过程的名称被调用. Ø 什么是触发器 触发器对表进行插入.更新.删 ...
- Oracle DBLINK 抽数以及DDL、DML操作
DB : 11.2.0.3.0 原库实例orcl:SQL> select instance_name from v$instance; INSTANCE_NAME--------------- ...