【题目】

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. 一起來玩鳥 Starling Framework(6)Juggler、Tween、以及DelayCall

    這篇開始來講Starling裡的Animation.Juggle是個簡單的Class,用來控制動畫的進行.他負責管理經由add()加進來的實現IAnimatable介面的物件,然後當Juggler的a ...

  2. IDEA Maven 下载jar

    转载地址:https://www.cnblogs.com/hongwz/p/5456578.html 本文主要介绍通过配置Maven来下载需要用的jar包. 比如我现在用到把字符串转成json对象,这 ...

  3. Hadoop Trash回收站使用指南

    转载:https://blog.csdn.net/sunnyyoona/article/details/78869778 我们在删除一个文件时,遇到如下问题,提示我们不能删除文件放回回收站: sudo ...

  4. 【OpenStack 虚拟机初始化user-data & Cloud-init】

    示例: import httplib import json import base64 tenant_id='xxx' token='xxx' compute_host="xxx" ...

  5. angular directive 的controllerAs的用法

    原文: https://stackoverflow.com/questions/31857735/using-controlleras-with-a-directive --------------- ...

  6. 转:http2的资料与使用

    https://imququ.com/post/http2-resource.html

  7. html5学习整理-0311

    整理一下今天所学的一些标签内容. 首先说一下DNS:全称Domain Name System,域名系统.是因特网上作为域名和IP地址相互映射的一个分布式数据库. URL协议:规定URL地址的格式,UR ...

  8. Web学习篇之---css基础知识(一)

    css基础知识(一) 1.css样式: 载入css样式有下面四种: 1).外部样式 2).内部样式 3).行内样式 4).导入样式 <link href="layout.css&quo ...

  9. 绘制 SVG

    工具网址:http://svg123.com/ 画布 点击画布,在页面的右侧我们可以调整画布属性 点击属性,滚动滑轮可以微调 或者直接双击修改属性值 工具栏 左侧工具栏 就像画图的工具栏一样 形状库 ...

  10. 基于python实现的DDoS

    目录 一个简单的网络僵尸程序 一个简单的DOS攻击程序 整合网络僵尸和DoS攻击--DDoS 代码地址如下:http://www.demodashi.com/demo/12002.html 本例子包含 ...