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 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的更多相关文章
- 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】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) ...
- 【一天一道LeetCode】#98. Validate Binary Search Tree
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 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 ...
- 【LeetCode】98. Validate Binary Search Tree 解题报告(Python & C++ & Java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 BST的中序遍历是有序的 日期 题目地址:ht ...
- leetcode笔记:Validate Binary Search Tree
一. 题目描写叙述 Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is ...
- 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 ...
- 【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 ...
- 【LeetCode OJ】Validate Binary Search Tree
Problem Link: https://oj.leetcode.com/problems/validate-binary-search-tree/ We inorder-traverse the ...
随机推荐
- GDAL创建图像提示Driver xxx does not support XXX creation option的原因
经常在群里有人问,创建图像的时候为什么老是提示下面的信息. CPLError: Driver GTiff does not support DCAP_CREATE creation option Wa ...
- Linux 环境下一些常用的命令(二)
11. chown命令 "chown"命令就是改变文件拥有者和所在用户组.每个文件都属于一个用户组和一个用户.在你的目录下,使用"ls -l",你就会看到像这样 ...
- 源码推荐:移动端商城(微信小程序源代码) WebView离线缓存
移动端商城(微信小程序源代码)(上传者:腾讯攻城师jack) 功能包括:商品橱窗,商品搜索,购物车,结账等功能. TableView嵌套webView自适应高度(上传者:linlinchen) tab ...
- java类加载器——ClassLoader
Java的设计初衷是主要面向嵌入式领域,对于自定义的一些类,考虑使用依需求加载原则,即在程序使用到时才加载类,节省内存消耗,这时即可通过类加载器来动态加载. 如果你平时只是做web开发,那应该很少会跟 ...
- x264源代码简单分析:滤波(Filter)部分
===================================================== H.264源代码分析文章列表: [编码 - x264] x264源代码简单分析:概述 x26 ...
- Effective C++ ——资源管理
条款13:以对象来管理资源 在C++中我们经常会涉及到资源的申请与申请,一般都是由关键字new 和 delete来操作的,两者都是成对存在的,缺一不可,否则会出现意想不到的问题,例如: class I ...
- 理解 Linux 的硬链接与软链接
Linux 的文件与目录 现代操作系统为解决信息能独立于进程之外被长期存储引入了文件,文件作为进程创建信息的逻辑单元可被多个进程并发使用.在 UNIX 系统中,操作系统为磁盘上的文本与图像.鼠标与键盘 ...
- Android Studio基本配置
主题设置 File→Settings- 添加第三方主题 网址:http://www.ideacolorthemes.org/home/ File→Import Settings- 设置控制台字体大小 ...
- 【Netty源码分析】ChannelPipeline(二)
在上一篇博客[Netty源码学习]ChannelPipeline(一)中我们只是大体介绍了ChannelPipeline相关的知识,其实介绍的并不详细,接下来我们详细介绍一下ChannelPipeli ...
- 解决Scrollview 嵌套recyclerview不能显示,高度不正常的问题
我们先看一个效果,问题说的就是中间的Grid效果在Scrollview 嵌套recyclerview显示问题,在Android Api 24是好的,不过在5,1,1版本(api 22)缺出现了问题 最 ...