【Leetcode】【Medium】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:
1、中序遍历二叉树,并将遍历结果装进数组。
2、检查数组是否由低到高依次排列。
注意:如果两个结点值一样,也判定为false;
代码:
/**
* 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) {
vector<int> nums;
stack<TreeNode*> nodes;
TreeNode* curNode = root; while (curNode || !nodes.empty()) {
while (curNode) {
nodes.push(curNode);
curNode = curNode->left;
} curNode = nodes.top();
nodes.pop();
nums.push_back(curNode->val);
curNode = curNode->right;
} for (int i = ; i < nums.size(); ++i) {
if (nums[i] <= nums[i-])
return false;
} return true;
}
};
解题思路2:
判断一个二叉树是不是二叉搜索树,除了判断是否满足 “左侧子树所有结点 < 当前结点 < 右侧子树所有结点”的方式外;
还可以判断是否满足:
1、子树的最左结点(最小结点),大于子树的“左父亲”;
2、子树中,每个结点大于自己的左儿子;
(“左父亲”:结点是自己的右子树的左父亲)
使用中序遍历的方式,判断一个子树是否满足二叉查找树:
0、遍历之前记录子树的“左父亲”的值;
1、判断左子树是否满足二叉查找树;
2、如果此结点没有左孩子,则判断此结点值,是否大于整个子树的“左父亲”(如果树没有“左父亲”,即没有父亲或者是父亲的左子树,则跳过此步);
3、如果此结点有左孩子,则判断此结点值,是否大于自己的左孩子;
4、将这个点作为“左父亲”,检查此结点的右子树是否是二叉查找树;
代码实现:
新建一个left指针,用于保存子树的“左父亲”或者结点左儿子;
如果当前结点是整个子树的最左结点,则left保存的是“左父亲”,结点需要>“左父亲”;
如果当前结点有左孩子,则left保存的是“左孩子”,结点需要>“左孩子”;
代码:
(注意left必须是地址形式在函数内可改,否则从左子树判定返回时,不能记录当前结点左儿子的数据)
/**
* 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) {
TreeNode* left = NULL;
return validate(root, left);
} bool validate(TreeNode* node, TreeNode* &left) {
if (node == NULL)
return true;
if (validate(node->left, left) == false)
return false;
if (left != NULL && left->val >= node->val)
return false;
left = node;
return validate(node->right, left);
}
};
【Leetcode】【Medium】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
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
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
- leetcode dfs Validate Binary Search Tree
Validate Binary Search Tree Total Accepted: 23828 Total Submissions: 91943My Submissions Given a bin ...
- LeetCode: Validate Binary Search Tree 解题报告
Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...
- [LeetCode] Lowest Common Ancestor of a Binary Search Tree 二叉搜索树的最小共同父节点
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...
- 39. Recover Binary Search Tree && Validate Binary Search Tree
Recover Binary Search Tree OJ: https://oj.leetcode.com/problems/recover-binary-search-tree/ Two elem ...
- [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] 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 ...
随机推荐
- DIY FRDM-KL25Z开发环境 -- 基于GNU工具链
IDE大行其道的今天,一键make极大的便利了开发的同时,也每每让各种半路出家的猿们遇到工具链的问题感到束手无策(不就是说自己嘛?^_^!!!).也玩过不少板子了,始终没去深究工具链方面的问题,对于嵌 ...
- PCB中的SOLD MASK和阻抗开窗
应用场合:1 PCB中的贴片的焊盘是不过油的,需要暴露出来用于焊接:对于电机驱动需要大电流的走线需要将走线保留暴露出来不过油,然后在上面走一层锡,增大锡箔,铜箔厚度,增大过流和防过热能力. 方法:先在 ...
- Delphi设置表格样式
//设置表格样式wordDoc.Tables.Item(1).Borders.Item(Word.WdBorderType.wdBorderLeft).LineStyle = Word.WdLineS ...
- XPath语法简介
XPath是一种在xml中查找信息的语言,具体可参考W3school XPath教程 XPath是以路径表达式来选择XML文档中的节点或节点集 === XPath节点(Node) 在 XPath 中, ...
- 关于webApi使用session
1.关于webApi使用session 在Global.asax中注册session添加以下代码 public override void Init() { //开启session this.Post ...
- Java - 如何进行安全发布
首先让我简单解释一下所谓"发布". 发布(publish),使对象可以在当前作用域之外的代码中可见,如果该对象被发布,则该对象的非私有域中引用的所有实例同样也会被发布. 不仅仅是作 ...
- nodejs图片上传
node中图片上传的中间键很多,比如formidable等,这里我们使用nodejs中的fs来实现文件上传处理: 1.安装中间键connect-multiparty npm install conne ...
- CentOS 7.2 安装 MySQL 5.6.24
说明:由于甲骨文公司收购了MySQL后,有将MySQL闭源的潜在风险,因此CentOS社区采用mysql的MariaDB分支的方式来避开这个风险. 所以需要先加入yum的仓库,才能利用yum来安装my ...
- Luogu1261: 服务器储存信息问题
题面 传送门 Sol 我们可以考虑每种\(rank\)的点\(u\)会被哪些点\(v\)感兴趣 如果\(dis[u][v]<\)所有满足\(rank\)大于\(rank[u]\)的点到\(v\) ...
- bzoj2119 股市的预测
传送门 感觉智商莫名其妙的就变低了……写这题的时候死活想不出来…… 做法其实不难…… 题目要求形如ABA的串的个数,我们可以枚举A的长度,利用标记关键点的方法统计答案.设枚举到的答案为k,每k个点标记 ...