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. 64.SHELL

    SHELL 1. crontab定时器 编辑使用crontab -e 一共6列,分别是:分 时 日 月 周 命令 查看使用crontab -l 删除任务crontab -r 查看crontab执行日志 ...

  2. Ubuntu 12 升级 SVN 1.6 到 1.8 版本

    在 Ubuntu 12 中使用 PhpStorm 10.x,CheckOut项目后,Event Log 提示: Subversion command line client version is to ...

  3. servlet过滤器实现维护项目

    最近公司需要系统维护,提出要建一个维护系统,要求: 1.访问公司域名跳到系统首页 2.点击首页的任意按钮给出维护提示信息 3.用户访问之前收藏的任意系统链接跳转到首页 下面介绍下用过滤器实现上述需求 ...

  4. centos bad ELF interpreter: No such file or directory

    sudo yum install glibc.i686

  5. mongodb university week4

    1.index Creation,background 如果在foreground运行index,会阻塞其他writer,如果background运行,会比较慢,但不会阻塞其他writer,可以并发写 ...

  6. Android入门之GPS定位详解

    一.LocationManager LocationMangager,位置管理器.要想操作定位相关设备,必须先定义个LocationManager. LocationManger locationMa ...

  7. .net Excel乱码

    .net 生成Excel乱码,如果你一直在乱码,怎么改GB2312和UTF-8也没用,那试试下面的方法吧  HttpContext.Current.Response.AppendHeader(&quo ...

  8. python trackback的使用心得

    以前在读代码的时候总是要花很久时间去找在哪里调用的某个函数,现在好了,直接使用:trackback.print_stack()就可以打印出调用栈了,在那个地方调用的一目了然... 而如果是异常栈的话就 ...

  9. centos: vmware 安装centos 7连不上网

    在vmware上安装centos 7时,安装完成后无法连接网络,通过搜索查找原来是有线网卡没有激活,默认情况下centos和redhat都不启用有线网卡,只能手动开启或者安装时直接启用. 1.进入命令 ...

  10. maven项目部分知识

    1.maven项目在pom.xml中用add dependencies加入jar包,搜索不到jar包的解决方案: Window  --> Show View --> Other输入mave ...