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) ...
随机推荐
- EL表达式介绍(1)
1. 产生背景: 在MVC体系结构中,JSP页面只是用来显示数据,但JSP脚本中的表达式功能不够强大,它不能直接对隐式对象中某对象的属性进行显示,需要配合 scriptlet才能显示数据,很是麻烦,如 ...
- 【Hadoop】HDFS原理、元数据管理
1.HDFS原理 2.元数据管理原理
- Spark1.0.0 编程模型
Spark Application能够在集群中并行执行,其关键是抽象出RDD的概念(详见RDD 细解),也使得Spark Application的开发变得简单明了.下图浓缩了Spark的编程模型. w ...
- Linux网络配置之虚拟网卡的配置(ubuntu 16.04)案例
sudo vim /etc/network/interfaces 标红的名称一定要一致 sudo vim /etc/resolv.conf 配置外网的ip(默认可以不填,系统自己获取)
- asp.net 中点击button弹出模式对话框,选择值后返回到页面中(window.showModalDialog实现)
<td>现从事专业</td><td> <asp:TextBox ID="tbMajor" runat="server ...
- Zend Framework(一) windows8.1下配置zend framework1.12
windows8.1下配置zend framework1.12配置步骤: 1. 下载 zend framework1.12库 2. 创建zend frameworkproject 2 ...
- React Native Android入门实战及深入源代码分析系列(2)——React Native源代码编译
本文为老曾原创.转载需注明出处:viewmode=contents">http://blog.csdn.net/minimicall?viewmode=contents 在上一节中,我 ...
- [linux]w命令和uptime命令查看系统负载
在Linux系统中查询系统CPU和内存的负载(使用率)时,我们通常习惯于使用top.atop或者ps,这篇文章将要给大家介绍如何使用w命令和uptime命令来查看系统的负载情况,对于uptime命令, ...
- android-seekbar的thumb图片不居中显示的处理办法
seekbar更换图片后,发现thumb的图片不会居中(竖直方向)显示了,代码如下: <SeekBar android:id="@+id/wb_seekbar" androi ...
- FileNotFoundException: http:\localhos46087125.jpg (文件名、目录名或卷标语法不正确
java.io.FileNotFoundException: http:\localhost:8080\ipms\upload\1332146087125.jpg (文件名.目录名或卷标语法不正确.) ...