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.

思路:中序遍历。当前值要比之前的小。

bool isValidBST(TreeNode* root) {
TreeNode * pPre = NULL;
TreeNode * pCur = root;
vector<TreeNode *> v; while(!v.empty() || NULL != pCur)
{
if(NULL != pCur)
{
v.push_back(pCur);
pCur = pCur->left;
}
else
{
if(pPre != NULL && v.back()->val <= pPre->val)
return false;
pPre = v.back();
v.pop_back();
pCur = pPre->right;
}
}
return true;
}

大神递归版:注意,每次左子树的值范围在最小值和根值之间,右子树的范围在根植和最大值之间。

public class Solution {
public boolean isValidBST(TreeNode root) {
return isValidBST(root, Long.MIN_VALUE, Long.MAX_VALUE);
} public boolean isValidBST(TreeNode root, long minVal, long maxVal) {
if (root == null) return true;
if (root.val >= maxVal || root.val <= minVal) return false;
return isValidBST(root.left, minVal, root.val) && isValidBST(root.right, root.val, maxVal);
}
}

【leetcode】Validate Binary Search Tree(middle)的更多相关文章

  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】二叉查找树 binary search tree(共14题)

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

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

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

  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). Assume a BST is defined as ...

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

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

  7. Leetcode 之Validate Binary Search Tree(53)

    判断是否是有效的二叉搜索树,即左子树的值小于根结点,右子树的值大于根结点.可以采用递归的方式来完成,递归时如何 传递有效的参数与根结点进行比较,是此题的难点. bool isValidBST(Tree ...

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

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

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

随机推荐

  1. Some Delphi tips

    ====================================conversion routines====================================Format('T ...

  2. jQuery属性选择器.attr()和.prop()两种方法

    在判断表单单选框是否被选中时,通常会想到使用$('#checkbox').attr('checked')来判断,但在一些情况下,你会发现这种方法并不管用,得到的是undefined. 原来jQuery ...

  3. [译]git push

    push就是把你本地仓储的commit传到远程仓储中去. 用法 git push <remote> <branch> push指定的分支到<remote>中去.   ...

  4. EF-在EF中运行sql语句

    DbRawSqlQuery<int> result2 = db.Database.SqlQuery<int>("SELECT count(*) FROM test.s ...

  5. 郝斌C语言代码

    #include<stdio.h> int main() { ; printf("%#x\n",a); ; } /* output 0xf; */ //(15)10= ...

  6. C++ Const引用详解

    (1)       在实际的程序中,引用主要被用做函数的形式参数--通常将类对象传递给一个函数.引用必须初始化. 但是用对象的地址初始化引用是错误的,我们可以定义一个指针引用. 1 int ival ...

  7. java实现文件单词频率统计 topN top K

    java 实现单词计数.top N 思路 先统计每个单词出现的个数 利用 TreeSet 的自动排序的功能 上代码 wordcount public void wordCount() { String ...

  8. iOS开发——UI基础-提示框

    提示框的种类有很多,废话不多说,直接上代码 一.文本提示框 运行结果如下: 代码实现如下: @interface ViewController () // 添加方法 - (IBAction)add; ...

  9. junk

    var mydate1 = new Date($('.stt').html()); var mydate2 = new Date($('.ett').html()); if (window.Activ ...

  10. java代码和spring框架读取xml和properties文件

    1.java文件读取properties文件 Properties props = new Properties(); try { //资源文件存放在类文件的根目录下.即是放在src下面.则不需要写路 ...