LeetCode: Validate Binary Search Tree [098]
【题目】
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]的更多相关文章
- LeetCode: Validate Binary Search Tree 解题报告
Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (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 ...
- [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 ...
- 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 ...
- 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 ...
- [leetcode]Validate Binary Search Tree @ Python
原题地址:https://oj.leetcode.com/problems/validate-binary-search-tree/ 题意:检测一颗二叉树是否是二叉查找树. 解题思路:看到二叉树我们首 ...
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
- 【leetcode】Validate Binary Search Tree
Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...
- 【LeetCode练习题】Validate Binary Search Tree
Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...
随机推荐
- [转载] 在Linux中,开机自动运行普通用户的脚本程序
FROM:http://blog.csdn.net/sinboy/article/details/2466225 FROM:http://www.2cto.com/os/201006/50680.ht ...
- plsql连接oracle数据库
步骤 (1)线上安装oracle数据库(已配好) (2)本地远程连接.安装oracle客户端(运行时) (3)安装plsql. (4)oracle客户端可以不用配置,直接在plsql中数据访问验证
- django dispatch
from django.views import View # 这里Home需要继承View class Home(View): # 这样这里就相当于一个装饰器的功能,可以自己定制化内容 def di ...
- cocos2d-x 重力感应 加速器的使用
CSDN开通已有两三年,今天作为一名刚入行的菜鸟写下自己的第一篇Blog. 刚好项目中须要用到重力感应,google下发现重力感应的使用很easy. 例如以下: 第一步: 在当前层开启重力感应.函数: ...
- 【Java】Java_02环境配置
JDK是什么?JRE是什么?JDK和JRE的区别? Java Runtime Environment (JRE) 包含: Java虚拟机.库函数.运行Java应用程序和Applet所必须文件 Java ...
- varchar2 和varchar区别
1.varchar2把所有字符都占两字节处理(一般情况下),varchar只对汉字和全角等字符占两字节,数字,英文字符等都是一个字节:2.VARCHAR2把空串等同于null处理,而varchar仍按 ...
- Android设置拍照或者上传本地图片
效果例如以下: 看代码: MainActivity类中: package com.example.ceshidemo; import java.io.ByteArrayOutputStream; im ...
- 将python对象序列化成php能读取的格式(即能反序列化到对象)
转载自:http://my.oschina.net/zuoan001/blog/94914 代码如下: #coding:utf-8 # vim: encoding=utf-8:ft=python:et ...
- mongoDB group命令详解
http://heipark.iteye.com/blog/1167948 http://rjhym.iteye.com/blog/1224200 http://blog.163.com/ ...
- 【转】SQL SERVER获取索引脚本
关于如何获取索引脚本的语句很多,上次在项目中需要去查询并获取索引脚本,所以写了一个简单的查询语句来进行获取. WITH idxcol AS ( SELECT ...