Validate Binary Search Tree——体现二查搜索树思想的一道题
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
思想其实很简单,但是我为啥就是想不到呢?????!!!!!
递归判断,递归时传入两个参数,一个是左界,一个是右界,节点的值必须在两个界的中间,同时在判断做子树和右子树时更新左右界。
需要考虑结点取INT_MAX 或者INT_MIN的情况,
相应的改成long long 以及 LONG_LONG_MAX 和LONG_LONG_MIN后提交通过。
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool check(TreeNode *node, long long leftVal, long long rightVal)
{
if (node == NULL)
return true; return leftVal < node->val && node->val < rightVal && check(node->left, leftVal, node->val) &&
check(node->right, node->val, rightVal);
} bool isValidBST(TreeNode *root) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
return check(root, LONG_LONG_MIN , LONG_LONG_MAX);
}
};
Validate Binary Search Tree——体现二查搜索树思想的一道题的更多相关文章
- [CareerCup] 4.5 Validate Binary Search Tree 验证二叉搜索树
4.5 Implement a function to check if a binary tree is a binary search tree. LeetCode上的原题,请参见我之前的博客Va ...
- [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] 98. 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]98. Validate Binary Search Tree验证二叉搜索树
Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...
- 098 Validate Binary Search Tree 验证二叉搜索树
给定一个二叉树,判断其是否是一个有效的二叉搜索树.一个二叉搜索树有如下定义: 左子树只包含小于当前节点的数. 右子树只包含大于当前节点的数. 所有子树自身必须也是二叉搜索树.示例 1 ...
- Leetcode98. Validate Binary Search Tree验证二叉搜索树
给定一个二叉树,判断其是否是一个有效的二叉搜索树. 假设一个二叉搜索树具有如下特征: 节点的左子树只包含小于当前节点的数. 节点的右子树只包含大于当前节点的数. 所有左子树和右子树自身必须也是二叉搜索 ...
- [LeetCode98]98. Validate Binary Search Tree判断二叉搜索树
判断二叉搜索树的方法是: 中序遍历形成递增序列 //全局变量记录中序遍历产生的序列,因为要递归,所以要用全局变量 List<Integer> list = new ArrayList< ...
- [LeetCode] Verify Preorder Sequence in Binary Search Tree 验证二叉搜索树的先序序列
Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary ...
- [LeetCode] 255. Verify Preorder Sequence in Binary Search Tree 验证二叉搜索树的先序序列
Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary ...
随机推荐
- BZOJ1072 排列perm 【状压dp】
Description 给一个数字串s和正整数d, 统计s有多少种不同的排列能被d整除(可以有前导0).例如123434有90种排列能 被2整除,其中末位为2的有30种,末位为4的有60种. Inpu ...
- js 生成二维码
$(".good_info").on('click',function () { var id = $(this).data('id'); var string = 'http:/ ...
- MySQL下创建序列及创建自定义函数方法介绍
工作过程中需要将基于DB2数据库的应用以及数据迁移到MySQL中去,在原应用中,大量使用了SEQUENCE,考虑尽量减少代码的修改,决定在迁移后的应用中继续保留SEQUENCE的使用,这就要求在MyS ...
- at org.codehaus.jackson.map.ser.BeanSerializer.serialize(BeanSerializer.java:142) :json转化“$ref 循环引用”的问题
原因: entity实体中存在@OneToMany,@ManyToOne注解,在转化json是产生了循环引用 报的错误 解决方法: springmvc @ResponseBody 默认的json转化用 ...
- OpenCV---圆检测
推文:Opencv2.4.9源码分析——HoughCircles 霍夫圆检测 加载一幅图像并对其模糊化以降噪 对模糊化后的图像执行霍夫圆变换 . 在窗体中显示检测到的圆. def detect_cir ...
- Asp.Net Core 依赖注入默认DI,Autofac注入
使用默认DI 修改Startup类方法ConfigureServices如下: public void ConfigureServices(IServiceCollection services) { ...
- 扫描线(线段树)+贪心 ZOJ 3953
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5572 Intervals Time Limit: 1 Second ...
- 字符串:AC自动机
给出一个字典和一个模式串,问模式串中出现几个字典中的单词 最后一行是大串,之前输入的是小串 #include<iostream> #include<cstdio> using ...
- 对WebView进行的一些设置
webView.getSettings().setJavaScriptEnabled(true); //使用setting WebSettings webSettings = webView.getS ...
- Linux网络知识
在思科上面模拟一下数据包的传递过程:一般上网使用的协议是tcp 交换机是一个2层的设备,它和Ip地址是没有关系的. 交换机上主要处理的是硬件地址(MAC),它只能分析到硬件地址,再到IP地址它就不管了 ...