【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 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.
confused what "{1,#,2,3}" means? > read more on how binary tree is serialized on OJ.
OJ's Binary Tree Serialization:
The serialization of a binary tree follows a level order traversal, where '#' signifies a path terminator where no node exists below.
Here's an example:
1
/ \
2 3
/
4
\
5
The above binary tree is serialized as "{1,2,3,#,#,4,#,#,5}".
解体思路:中序遍历BST得到的递增的数组,可以使用栈中序遍历得到数组,比较数组元素来判断是否是BST。
class Solution {
public:
bool isValidBST(TreeNode *root) {
vector<TreeNode*> stack;
TreeNode* node = root;
vector<int> v;
while (stack.size()> || node!=NULL) {//inorder
if (node!=NULL){
stack.push_back(node);
node = node->left;
}else{
node = stack.back();
stack.pop_back();
v.push_back(node->val);
node = node->right;
}
}
for(int i=; v.size()> && i<v.size()-; i++)
if (v[i] >= v[i+])
return false;
return true;
}
};
疑惑:以下代码采用递归,但是有测试用例通不过,没找到原因。
/**
* 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) {
return isValidBST(root, INT_MIN, INT_MAX);
} bool isValidBST(TreeNode *root, int lower, int upper){
if(root == nullptr)
return true;
return root->val >= lower && root->val <= upper && isValidBST(root->left, lower, root->val)
&& isValidBST(root->right, root->val, upper);
}
};
|
67 / 74 test cases passed.
|
Status:
Wrong Answer |
|
Submitted: 3 hours, 26 minutes ago
|
| Input: | {2147483647} |
| Output: | false |
| Expected: | true |
【leetcode】 Validate Binary Search Tree的更多相关文章
- 【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 ——合法二叉树
[题目] Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defin ...
- 【leetcode】Validate Binary Search Tree(middle)
Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...
- 【题解】【BST】【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 ...
- 【LeetCode】Validate Binary Search Tree 二叉查找树的推断
题目: Given a binary tree, determine if it is a valid binary search tree (BST). 知识点:BST的特点: 1.一个节点的左子树 ...
- 【Leetcode】【Medium】Validate Binary Search Tree
Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...
- 【LeetCode】二叉查找树 binary search tree(共14题)
链接:https://leetcode.com/tag/binary-search-tree/ [220]Contains Duplicate III (2019年4月20日) (好题) Given ...
- 【LeetCode】173. Binary Search Tree Iterator 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 保存全部节点 只保留左节点 日期 题目地址:http ...
- 【leetcode】Recover Binary Search Tree
Recover Binary Search Tree Two elements of a binary search tree (BST) are swapped by mistake. Recove ...
随机推荐
- TMS320VC5509片内ADC采集
1. ADC采集比较简单,内部的10位的ADC,AIN0-AIN3的输入,主要是用的CSL的库函数#include <csl_adc.h> ; Uint16 samplestoraage[ ...
- Invitation Cards POJ-1511 (spfa)
题目链接:Invitation Cards 题意: 给出一张有向图,现在要求从1到其他所有的结点的最小路径和与从所有其他结点到1的最小路径和之和. 题解: 求最小路径可以用SPFA来求解.从1到其他结 ...
- 使用 Vue.js 2.0+ Vue-resource 模仿百度搜索框
使用 Vue.js 2.0 模仿百度搜索框 <!DOCTYPE html> <html> <head> <meta charset="utf-8&q ...
- C#对战小游戏,持续更新(里面暂无内容,标记插眼)
做的乱七八糟的 很明显的一点,对集合.数组.类的理解和运用 很差.很差.很差 今儿不做了,马德,头都肿大了 休息一下,捋一捋
- 阿里云 ECS 监控报警设置
1.阿里云监控项说明 https://helpcdn.aliyun.com/document_detail/43505.html 2.监控设置 3.报警规则 4.设置阈值 5.确定即可. 6.效果图
- Ubuntu16.4下QT配置opencv3.1+FFmpeg
安装依赖环境 sudo apt-get install build-essential sudo apt-get install cmake git libgtk2.0-dev pkg-config ...
- 01_python2.x和python3.x中range()的区别
Py2.x 1) .range 和xrange都是经常使用的,特别是range()返回一个列表 2) .xrange()一般用来创建迭代对象 Py3.x xrange()不存在了,只有range()而 ...
- 关于go v1.11安装后出现不能正常运行测试程序的问题
本人最近安装go1.11后出现上述问题,没有找到原因,可能之前安装过的旧的版本在windows下环境变量设置出现了问题,修改后仍然无效,后来删除所有安装版本,及go环境变量,重新下载1.10版本进行安 ...
- 初识kibana
前言: 什么是Kibana?? Kibana是一个开源的分析与可视化平台,设计出来用于和Elasticsearch一起使用的.你可以用kibana搜索.查看.交互存放在Elasticsearch索引里 ...
- html5实现拖拽上传头像
1. 将客户端(本地电脑)中的图片拖到网页中 要点: html5 拖放, FileReader html: <div id="container" ondrop=" ...