【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 ...
随机推荐
- 【php增删改查实例】第七节 - 部门管理模块(画一个datagrid表格)
在easyui中,datagrid组件需要用一个table标签去渲染. <table id="grid0" title="部门管理" class=&quo ...
- 洛咕3312 [SDOI2014]数表
洛咕3312 [SDOI2014]数表 终于独立写出一道题了...真tm开心(还是先写完题解在写的) 先无视a的限制,设\(f[i]\)表示i的约数之和 不妨设\(n<m\) \(Ans=\su ...
- CS190.1x-ML_lab1_review_student
这是CS190.1x第一次作业,主要教你如何使用numpy.numpy可以说是python科学计算的基础包了,用途非常广泛.相关ipynb文件见我github. 这次作业主要分成5个部分,分别是:数学 ...
- [LOJ#2878]. 「JOISC 2014 Day2」邮戳拉力赛[括号序列dp]
题意 题目链接 分析 如果走到了下行车站就一定会在前面的某个车站走回上行车站,可以看成是一对括号. 我们要求的就是 类似 代价最小的括号序列匹配问题,定义 f(i,j) 表示到 i 有 j 个左括号没 ...
- pt-online-schema-change的实现原理
pt-online-schema-change用于MySQL的在线DDL. 下面结合官方文档和general log来分析其实现原理. 测试表 mysql> show create table ...
- ANSYS渡槽槽身动水压力的施加(2)——U型渡槽
U型渡槽动水压力荷载施加命令及说明 程序中需要用到ANSYS重启动,因为需提取前一步加速度结果以施加部分动水压力: 默认Y方向为重力方向,X方向为横槽向,Z方向为纵槽向: 需准备地震波文件: 需先将槽 ...
- 转 ssh-keygen 的 详解
为了让两个linux机器之间使用ssh不需要用户名和密码.所以采用了数字签名RSA或者DSA来完成这个操作. 模型分析 假设 A (192.168.20.59)为客户机器,B(192.168.20.6 ...
- yocto-sumo源码解析(三):oe-setup-builddir
该脚本的主要功能就是创建构建目录并准备一些配置文件,比如conf/local.conf,conf/bblayer.conf 1. 检测BUILDDIR环境变量是否设置好(在本系列分享第二节已经知道:B ...
- Alpha阶段结束分数(百分制)
- Daily Scrum 12.22
姓名 上周末任务 今日任务 刘垚鹏 完善和增加quiz页面的过滤功能 完善和增加quiz页面的过滤功能 王骜 对问答功能的修复 对问答功能的修复 林旭鹏 存储文件路径太长导致bug修复 存储文件路径太 ...