[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.
这是easy题么=_= 被虐得好惨,开始想得简单,不就是验证BST咩,递归的话只用判断左右孩子是不是BST就行了。。。这就错了,孩子是BST并不能保证整颗树就是BST啊。比如下图
5
/ \
3 6
/ \ / \
1 8 4 7
5的左右孩子都分别是合法的BST,但是整颗树却不是,因为6的左孩子比5小;3的右孩子比5大。
所以在递归检测的时候,不仅仅要保证当前的树是BST,也要保证当前的树不破坏原有的BST结构,怎么办呢?
认真思考BST的结构性质会发现,BST就好像把一串数字做了很多切分,一部分放左变一部分放右边,一直这样递归的切下去。而这个切法可不是乱切,他是永远保持 “小的放左边,大的放右边” 的,这样切就有个性质,就是被切出来的每一段区域,都有严格的上下界。
3
/ \
1 5
/ \ / \
0 2 4 6
上图是一颗合法的BST,如果我们将其按照中序遍历方式展开可得:0 1 2 3 4 5 6
注意观察1,3,5 把序列切分成了4个区域,每个区域有一个数子分别是0,2,4,6。除了0没有下界,6没有上界以外,2和4都有明确的上下界。
所以在递归检测当中,我们应该同时传递上下界信息。
思路:在递归检测左右孩子的同时传递上下界信息,在递归调过程中更新上下界信息。
举个栗子:以上图为例,以根节点启动算法时,上下界信息为空,在检测1,3,5这个结构是否为合法BST后,递归的检测根节点为1的树(左孩子)并传入上界信息3;同样,递归检测根节点为5的树(右孩子)并传入下界信息3...
在递归过程中更新上下界信息,比如在检测到2这个节点的时候,更新了下界为1,而上界不用更新还是3。
bool validIter(TreeNode *node, TreeNode *left, TreeNode *right) {
if (!node) return true;
bool validLeft = true;
bool validRight = true;
if (node->left) {
validLeft = (node->val > node->left->val);
if (left) {
validLeft = validLeft && (node->left->val > left->val);
}
}
if (node->right) {
validRight = (node->val < node->right->val);
if (right) {
validRight = validRight && (node->right->val < right->val);
}
}
if (validLeft && validRight) {
return validIter(node->left, left, node) && validIter(node->right, node, right);
}
return false;
}
bool isValidBST(TreeNode *root) {
return validIter(root, NULL, NULL);
}
要点:检测当前树是否为BST的时候还要检测是否违反了上下界的约束。
解法二:使用中序便利
上面的递归解法不太好想,我跌跌撞撞的提交了两次才对算法有了较深的理解。前面也说到,BST按照中序便利的话将会产生一个有序的序列,这是BST的性质。那为何不利用这个性质:先中序遍历产生数组,然后检查数组是否有序和是否有重复。
这个思路就是这么简单。。。中序遍历然后检查数组,所以才是easy题啊 (逃
void midOrderTraversal(TreeNode *node, vector<int> &s) {
if (!node) return;
midOrderTraversal(node->left, s);
s.push_back(node->val);
midOrderTraversal(node->right, s);
}
bool isValidBST(TreeNode *root) {
if (!root) return true;
vector<int> s;
midOrderTraversal(root, s);
for (int i = ; i < s.size(); i++) {
if (s[i] < s[i-] || s[i] == s[i-]) {
return false;
}
}
return true;
}
[LeetCode] Validate Binary Search Tree (两种解法)的更多相关文章
- 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 [098]
[题目] Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defin ...
- 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) ...
随机推荐
- POJ 2631 DFS+带权无向图最长路径
http://poj.org/problem?id=2631 2333水题, 有一个小技巧是说随便找一个点作为起点, 找到这个点的最远点, 以这个最远点为起点, 再次找到的最远点就是这个图的最远点 证 ...
- time()函数,dirname(__FILE__) 的使用总结
time()函数将返回从1970-1-1 0:0:0到当前时间的秒数.(整型) dirname(__FILE__) php中定义了一个很有用的常数,即 __file__ 这个内定常数是当前php程序的 ...
- CLR via C# 随记
使用C# 编译器的方法: 1.csc.exe位于C:\Windows\Microsoft.NET\Framework\vxxxxx下面,将对应版本的路径配置到环境变量path中,如将";C: ...
- Unity3D研究院之拓展系统自带组件的Inspector视图
转自 http://www.xuanyusong.com/archives/3455 using UnityEngine; using System.Collections; using UnityE ...
- nginx做本地目录映射
有时候需要访问服务器上的一些静态资源,比如挂载其他设备上的图片到本地的目录,而本地的目录不在nginx根目录下,这个时候就需要简单的做一下目录映射来解决,比如想通过浏览器http://ip/image ...
- Delphi中Format与FormatDateTime函数详解
copy:http://hi.baidu.com/yunfanleo/blog/item/0c51d9cdbc0531550eb34558.html Format是一个很常用,却又似乎很烦的方法,本人 ...
- Centos 用户登录失败N次后锁定用户禁止登陆
针对linux上的用户,如果用户连续3次登录失败,就锁定该用户,几分钟后该用户再自动解锁 Linux有一个pam_tally2.so的PAM模块,来限定用户的登录失败次数,如果次数达到设置的阈值,则锁 ...
- 前端js模版 预编译工具Tmod js使用入门
1. 安装node js , 2. 用 npm install -g tmodjs 命令安装tmod 3.了解参数配置 4.运行测试例子->命令窗切换到当前文档位置 --->执行tomd ...
- css 发光字效果
<!DOCTYPE html> <html> <head> <!-- 超酷炫的CSS3发光字体,可自定义喔!!--> <meta http-equ ...
- 获取指定文件下的所有file文件
/** * 描述:获取所有的文件列表 * @param file * @param list * @return */ private List<File> getAllFiles(Fil ...