【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.
【解析】
题意:判断一个二叉树是否为二分查找树。
何为二分查找树?1) 左子树的值都比根节点小;2) 右子树的值都比根节点大;3) 左右子树也必须满足上面两个条件。
需要注意的是,左子树的所有节点都要比根节点小,而非只是其左孩子比其小,右子树同样。这是很容易出错的一点是,很多人往往只考虑了每个根节点比其左孩子大比其右孩子小。如下面非二分查找树,如果只比较节点和其左右孩子的关系大小,它是满足的。
5
/ \
4 10
/ \
3 11
【错误代码示范】【NA】
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public boolean isValidBST(TreeNode root) {
if (root == null) return true;
if (root.left != null && root.val <= root.left.val) return false;
if (root.right != null && root.val >= root.right.val) return false;
return isValidBST(root.left) && isValidBST(root.right);
}
}
正确解法:中序遍历
二分查找树的中序遍历结果是一个递增序列。
/**
* 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 isValidBST(TreeNode *root) {
if(root==NULL) return true;
bool res = true;
res&=isValidBST(root->left);
if(pre!=NULL&&pre->val>=root->val) res=false;
pre=root;
res&=isValidBST(root->right);
return res;
}
TreeNode *pre=NULL;
};
【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 [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 验证二叉搜索树
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 dfs Validate Binary Search Tree
Validate Binary Search Tree Total Accepted: 23828 Total Submissions: 91943My Submissions Given a bin ...
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
随机推荐
- mysql再次安装问题
安装过一次mysql的电脑,想再安装或更换其它版本的mysql.在重新安装的最后一步,总会出现这样的问题. 网上说法也很多,什么删除注册表了等等.这都是狗屁. 真正的做法是找到C盘下的隐藏文件夹Pro ...
- e.keyCode和e.which使用
1. 不使用jquery获取keyCode var key = 'which' in e ? e.which : e.keyCode;//或者var key = e.which || e.keyCod ...
- 急速安装Ubuntu/windows双操作系统
本文出自:http://www.cnblogs.com/svitter FAQ 因为很多人都不看FAQ,比如像我,所以直接把FAQ写在最前面,然后把正文卸载最后面逼你看- - 常用软件下载(官网) d ...
- nodeJS学习(8)--- WS/...开发 NodeJS 项目-节3 <使用 mongodb 完整实例过程>
使用 mongodb 的小系统 参考:https://my.oschina.net/chenhao901007/blog/312367 1. Robomongo 创建项目的数据库和数据表 参考:htt ...
- iPhone深度学习-ARM
平台 xCode 5.0 iPhone 4 在Building setting中的 Architectures 部分,有这么一个选项 Architectures,这里有一些选项是 Armv7 和Arm ...
- 调用Outlook发送邮件
#region 查找与指定文件关联在一起的程序的文件名 /// <summary> /// 查找与指定文件关联在一起的程序的文件名 /// </summary> /// < ...
- [论文]A Link-Based Approach to the Cluster Ensemble Problem
论文作者:Natthakan Iam-On, Tossapon Boongoen, Simon Garrett, and Chris Price 下次还是在汇报前先写了论文总结,不然有些点汇报时容易忘 ...
- python print的参数介绍
参考print的官方文档 print(...) print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False) Prints th ...
- TDictionary字典 对象的释放。。。
type TRen = record name: string; age: Integer; end; type TPeople = class private Fname: string; Fage ...
- 广州地区常用的DNS解析服务器
广州电信DNS: 首选:202.96.128.143 备用:202.96.128.68 首选:202.96.134.133 备用:202.96.128.166 首选:61.144.56.100备用:6 ...