【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) ...
随机推荐
- .NET中 使用数组的注意事项
1.初始值问题 对于int.double.float等一些值类型数组,没有赋值的情况下, 默认值是0: 而对于String 等引用类型,初始值为null. 2.IndexOutOfRangeExcep ...
- iConvert Icons 图标转换生成利器,支持Windows, Mac OS X, Linux, iOS,和Android等系统
这是一款在线图标转换工具,生成的图标支持Windows, Mac OS X, Linux, iOS, 和 Android等主流系统. 可以上传图标文件转化成另一个平台下的图标文件,例如将windows ...
- JQuery Validate使用总结
本文参考了 http://www.cnblogs.com/linjiqin/p/3431835.html 可以在mvc 或webform项目中使用,可以方便快捷的对前端表单进行校验 一.导入两个js ...
- C# Datetime类常用技巧
C#类常用技巧 //今天DateTime.Now.Date.ToShortDateString();//昨天,也就是今天的日期减一DateTime.Now.AddDays(-1).ToShortDat ...
- 打包程序tar
tar [选项] tar文件 [目录文件] 常用选项如下所述: -c:创建新的归档文件 -d:检查归档文件与指定目录的差异 -r:向归档文件中追加文件 -v:显示命令的执行日期 -u:只有当需要追加 ...
- Maven的HTTP代理设置
http://blog.sina.com.cn/s/blog_4f925fc30102ed3y.html 第一.检测本地网络是否不能直接访问Maven的远程仓库,命令为ping repo1.mav ...
- Linux下的Source命令及脚本的执行方式解析
Linux Source命令及脚本的执行方式解析 http://blog.csdn.net/wangyangkobe/article/details/6595143 当我修改了/etc/profile ...
- iostat命令简单说说
tps: 每秒钟发送到的I/O请求数. Blk_read /s: 每秒读取的block数 Blk_wrtn/s: 每秒写入的block数 Blk_read: 读入的block总数 Blk_wrtn: ...
- centos+php+coreseek+sphinx+mysql之二sphinx配置篇
先进入文件夹进行以下操作 cd /usr/local/coreseek/etc cp sphinx.conf.dist sphinx.conf source src1 { sql_host = 127 ...
- 在 Windows 7 環境安裝 Python 2.6.6
目前 Python 的最穩定的版本是 2.7.3 及 3.2.3,因為 2.x 與 3.x 語法並不是完全相容,在各版本之間也有些差異,所以建議還是各自安裝需要的版本… 艾小克工作環境是使用 2.6 ...