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的更多相关文章

  1. 【leetcode】Validate Binary Search Tree

    Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...

  2. 【LeetCode】Validate Binary Search Tree ——合法二叉树

    [题目] Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defin ...

  3. 【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 ...

  4. 【题解】【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 ...

  5. 【LeetCode】Validate Binary Search Tree 二叉查找树的推断

    题目: Given a binary tree, determine if it is a valid binary search tree (BST). 知识点:BST的特点: 1.一个节点的左子树 ...

  6. 【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 ...

  7. 【LeetCode】二叉查找树 binary search tree(共14题)

    链接:https://leetcode.com/tag/binary-search-tree/ [220]Contains Duplicate III (2019年4月20日) (好题) Given ...

  8. 【LeetCode】173. Binary Search Tree Iterator 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 保存全部节点 只保留左节点 日期 题目地址:http ...

  9. 【leetcode】Recover Binary Search Tree

    Recover Binary Search Tree Two elements of a binary search tree (BST) are swapped by mistake. Recove ...

随机推荐

  1. POJ 3278&&2049&&3083

    这次的题目叫图的深度&&广度优先遍历. 然后等我做完了题发现这是DFS&&BFS爆搜专题. 3278:题目是经典的FJ,他要抓奶牛.他和牛(只有一头)在一条数轴上,他们 ...

  2. LoRa---官方例程移植

    SX1278芯片上移植Semtech官方PING-PONG例程 移植环境:keil5.20 硬件平台:stm32f051+sx1278 1.下载源码:Semtech官网下载最新例程链接:http:// ...

  3. [UOJ#276][清华集训2016]汽水[分数规划+点分治]

    题意 给定一棵 \(n\) 个点的树,给定 \(k\) ,求 \(|\frac{\sum w(路径长度)}{t(路径边数)}-k|\)的最小值. \(n\leq 5\times 10^5,k\leq ...

  4. nginx 跳转

    nginx 跳转 一.需求:当需要在别的机访问本机房的服务器问题. 虚拟主机头配置 server { listen ; server_name test.zlx.com; location / { i ...

  5. ROCKETMQ——2主2从集群部署

    1.压缩包准备两台服务器镜像操作cd /optmkdir softcd soft将两个压缩包复制到 soft目录unzip apache-maven-3.2.2-bin.zipunzip rocket ...

  6. nodejs 监控代码变动实现ftp上传

    被动模式下 //https://www.npmjs.com/package/watch //文件同步功能 var watch = require('watch'); var path = requir ...

  7. 新手向:从不同的角度来详细分析Redis

    最近对华为云分布式缓存产品Redis做了一些研究,于是整理了一些基本的知识拿出来与大家分享,首先跟大家分享的是,如何从不同的角度来详细使用Redis. 小编将从以下9个角度来进行详细分析,希望可以帮到 ...

  8. N的阶乘:高精度

    N的阶乘 题目描述  输入一个正整数N,输出N的阶乘. 输入描述: 正整数N(0<=N<=1000) 输出描述:  输入可能包括多组数据,对于每一组输入数据,输出N的阶乘 示例1 输入 4 ...

  9. Linux内核分析作业 NO.1

    通过汇编一个简单的C程序,分析汇编代码理解计算机是如何工作的 于佳心 原创作品转载请注明出处 <Linux内核分析>MOOC课程http://mooc.study.163.com/cour ...

  10. HTTP 和 HTTPS 直观上看哪里不一样了

    1. 我在自己搭建的 HTTP 网站上进行登陆测试 填写账号和密码,账号:123456 ,密码:654321 (当然是乱填的,只为了看传输数据) 点击登录,用wireshark抓包看看传输的数据 2. ...