题目链接

  题目要求:

  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.

  OJ's Binary Tree Serialization:

  The serialization of a binary tree follows a level order traversal, where '#' signifies a path terminator where no node exists below.

  Here's an example:

   1
/ \
2 3
/
4
\
5

  The above binary tree is serialized as "{1,2,3,#,#,4,#,#,5}".

  用惯性思维(即判断某节点的值与其左右子节点的值得关系)去解决这道题,相对麻烦些(花了比较多的时间,最后还是很难兼顾各种情况)。我们可以用中序遍历的方法将整棵树输出,然后再判断输出序列是否是递增序列就行了。

  还有一篇博文的想法特别好,我们要将关注点放在节点本身,然后不断更新它的上下限。

  具体程序如下:

 /**
* 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 isValidBST(TreeNode* root) {
return isValidBSTSub(root, LONG_MIN, LONG_MAX);
} bool isValidBSTSub(TreeNode *tree, long alpha, long beta)
{
if(!tree)
return true; if(tree->val > alpha && tree->val < beta)
return isValidBSTSub(tree->left, alpha, tree->val) &&
isValidBSTSub(tree->right, tree->val, beta);
else
return false;
}
};

  上边程序用到了LONG_MIN、LONG_MAX,主要是为了应付比较极端的测试集。下图是C/C++中个数据类型的最大值宏定义列表:

  

LeetCode之“树”:Validate Binary Search Tree的更多相关文章

  1. Leetcode 笔记 98 - Validate Binary Search Tree

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

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

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

  3. 【LeetCode】98. Validate Binary Search Tree (2 solutions)

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

  4. 【一天一道LeetCode】#98. Validate Binary Search Tree

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  5. LeetCode OJ:Validate Binary Search Tree(合法的二叉搜索树)

    Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...

  6. 【LeetCode】98. Validate Binary Search Tree 解题报告(Python & C++ & Java)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 BST的中序遍历是有序的 日期 题目地址:ht ...

  7. leetcode笔记:Validate Binary Search Tree

    一. 题目描写叙述 Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is ...

  8. LeetCode OJ 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 ...

  9. 【LeetCode】98. Validate Binary Search Tree

    题目: Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is define ...

  10. 【LeetCode OJ】Validate Binary Search Tree

    Problem Link: https://oj.leetcode.com/problems/validate-binary-search-tree/ We inorder-traverse the ...

随机推荐

  1. 关于前端HTML你需要知道的一切

    1.H系列标签(Header 1~Header 6) 作用: 用于给文本添加标题语义 格式: <h1>xxxxxx</h1> 注意点: H标签是用来给文本添加标题语义的, 而不 ...

  2. Linux命令行总结

    1.修改同一目录下所有图片的尺寸(宽x高) 长宽比不变&长宽比改变 find ./ -name '*.jpg' -exec convert -resize 600x480 {} {} \; f ...

  3. 剑指Offer——知识点储备-设计模式

    剑指Offer--知识点储备-设计模式 设计模式 设计模式的六大原则 (1)单一职责原则(有且仅有一个原因引起类的变化): (2)里氏替换(任何父类出现的地方子类都可以替换): (3)依赖倒置(依赖抽 ...

  4. LeakCanary使用手册

    demo 一个非常简单的 LeakCanary demo: https://github.com/liaohuqiu/leakcanary-demo 开始使用 在 build.gradle 中加入引用 ...

  5. 验证码程序Demo

    小伙伴都有这样的经历,册各种网站,总是输不对验证码,双十一那天狂买的小伙伴是不是对输入验证码有着不一样的感触呢,以前觉得验证码真是个麻烦鬼,一个不小心,一个眼拙,哎呦,没有输入正确,又是一阵子大眼瞪小 ...

  6. 剑指Offer——Trie树(字典树)

    剑指Offer--Trie树(字典树) Trie树 Trie树,即字典树,又称单词查找树或键树,是一种树形结构,是一种的单词.对于每一个单词,我们要判断他出没出现过,如果出现了,求第一次出现在第几个位 ...

  7. 【移动开发】自定义ProgressBar

    <ProgressBar android:layout_centerInParent="true" android:layout_width="30dp" ...

  8. 算法之路(二)呈现O(logN)型的三个算法

    典型时间复杂度 我们知道算法的执行效率,可以从它的时间复杂度来推算出一二.而典型的时间复杂度有哪些类型呢? 由上图,可以看出,除了常数时间复杂度外,logN型的算法效率是最高的.今天就介绍三种非常ea ...

  9. android GifView分享

    gif图动画在android中还是比较常用的,比如像新浪微博中,有很多gif图片,而且展示非常好,所以我也想弄一个.经过我多方的搜索资料和整理,终于弄出来了,其实github上有很多开源的gif的展示 ...

  10. Java:使用匿名内部类在方法内部定义并启动线程

    下面的代码展示了在一个方法中,通过匿名内部类定义一个Thread,并Override它的run()方法,之后直接启动该线程. 这样的代码可用于在一个类内部通过另起线程来执行一个支线任务,一般这样的任务 ...