【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) ...
随机推荐
- jquery 分页控件功能
<script> //分页 function getPageNum(num) { $("#PageNum ul" ...
- 软件工程 speedsnail 冲刺4
2015-5-8 完成任务:学习了黑马android教学视频7.8.9集,对布局和计划做了调整: 遇到问题: 问题1 异常 Warning: Activity not started, its cur ...
- [leetcode]_Container With Most Water
题目:在二维坐标系下,有很多个挡板,有两个挡板之间能够积蓄的水的最大面积.如下图所示: 思路:我只想到暴力解法,用O(n2)的时间复杂度算出任意两个挡板形成的面积,这必须的过不了. 优化解法:O(n) ...
- VS2010在非IE浏览器下调试Silverlight程序
以Chrome为例: 第一步:在程序中设置断点. 第二步:右键点击web应用程序的起始页(.html或.aspx文件),选择"浏览方式",选中Chrome或其它非IE浏览器,点&q ...
- 自定义Toast的显示效果
Activity: package com.example.editortoast; import android.app.Activity; import android.os.Bundle; im ...
- PHP 简单实现MySQL数据搜索、添加数据功能 以设备管理为例
测试截图: 数据库bzec ,表shebeidangan 列sb_name,sb_numandtype,sb_home,sb_usedate,sb_address,sb_updatetime,sb_r ...
- 向Windows 日志管理器写入系统程序日志信息
标准样例代码: using System; using System.Collections.Generic; using System.ComponentModel; using System.Da ...
- python解析页面上json字段
一般来说,当我们从一个网页上拿下来数据,就是一个字符串,比如: url_data = urllib2.urlopen(url).readline() 当我们这样得到页面数据,url_data是全部页面 ...
- 分享我常用的一些JS验证和函数
下面是我常用一些JS验证和函数,有一些验证我直接写到了对象的属性里面了,可以直接通过对象.方法来调用//浮点数除法运算 function fdiv(a, b, n) { if (n == undefi ...
- 模糊查询&&日期时间操作
一.模糊查询 1.采用“_”.“%”通配符进行查询 select * from Students where stu_name like '张_';--一个‘_’表示一个字符 select * fro ...