【题目】

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.

【题意】

给定一棵二叉树,推断是不是合法的二叉搜索树

【思路】

依据二叉搜索树定义,递归推断就可以

【代码】

/**
* 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 isValid(TreeNode*root, int lowBound, int upBound){
//每棵树取值都有上边界和下边界
if(root==NULL)return true;
//推断节点值是否在合法的取值区间内
if(!(root->val>lowBound && root->val<upBound))return false; //推断左子树是否合法
if(root->left){
if(root->left->val >= root->val || !isValid(root->left, lowBound, root->val))return false;
}
//推断右子树
if(root->right){
if(root->right->val <= root->val || !isValid(root->right, root->val, upBound))return false;
} return true;
} bool isValidBST(TreeNode *root) {
return isValid(root, INT_MIN, INT_MAX);
}
};

LeetCode: Validate Binary Search Tree [098]的更多相关文章

  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 defined as ...

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

  4. 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[具体分析]

    Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less th ...

  6. [leetcode]Validate Binary Search Tree @ Python

    原题地址:https://oj.leetcode.com/problems/validate-binary-search-tree/ 题意:检测一颗二叉树是否是二叉查找树. 解题思路:看到二叉树我们首 ...

  7. Leetcode 笔记 98 - Validate Binary Search Tree

    题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...

  8. 【leetcode】Validate Binary Search Tree

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

  9. 【LeetCode练习题】Validate Binary Search Tree

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

随机推荐

  1. 在eclipse上配置tomcat

    Eclipse中Tomcat的配置及简单例子 Eclipse中Tomcat的配置是很简单的一个工作,作为一名刚刚起步的编程菜鸟,我将这个配置的过程和简单的例子写下来记录,也希望能给像我怎样的新手一些帮 ...

  2. Python 自用代码(某方标准类网页源代码清洗)

    用于mongodb中“标准”数据的清洗,数据为网页源代码,须从中提取: 标准名称,标准外文名称,标准编号,发布单位,发布日期,状态,实施日期,开本页数,采用关系,中图分类号,中国标准分类号,国际标准分 ...

  3. go同一个目录下的go文件里面不能有多个package

    原文: https://golang.org/doc/code.html#PackagePaths -------------------------------------------------- ...

  4. 转: 私人珍藏的Chrome插件,吐血推荐

    转:来自 http://stormzhang.com/devtools/2016/01/15/google-chrome-extension/

  5. Ajax库的编写及使用

    ajax使用在服务器端. ajax.js function ajax(url,fnSucc,fnFail) { //1.创建ajax对象 var oAjax = null; if(window.XML ...

  6. Quora使用到的技术

    本文主要参考了Phil Whelan的这篇文章<Quora’s Technology Examined>.关于Quora是个什么网站我就不多说了,国内对他的C2C网站叫“知乎”.呵呵.我们 ...

  7. es6模块学习总结

    模块功能主要由两个命令构成:export和import. export用于输出对外接口,improt用于输人接口 exprot 可以输出变量,也可以输出函数.类. 输出变量的三种写法 // 写法一ex ...

  8. iOS开发-关闭/收起键盘方法总结

    前言:作为IOS开发人员,需要经常和表单打交道.因此我对收起键盘的方法作了下总结,IOS收起键盘有三种方法(如果有其它收起键盘的方法请在留言区指错). 收起键盘的方法: 1.点击Return按扭时收起 ...

  9. <转>巧用notepad++ 批量转换ansi 和 utf8

    原方出处:http://stackoverflow.com/questions/7256049/notepad-converting-ansi-encoded-file-to-utf-8 Here s ...

  10. 流水线策略 相关算法 Tomasulo算法与记分牌调度算法

    设计流水线策略时,可参考 Tomasulo算法与记分牌调度算法  (这两个是霍老师推荐的算法,自己未了解过)