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?
二. 题目分析
这道题的大意是,推断一个二叉查找树是否合法的,这个能够依据二叉查找树的定义来进行推断,即一个内部结点的值要大于左子树的最大值,同一时候要小于右子树的最大值。
依据这个定义。能够递归得进行推断,这样的方法得时间复杂度为O(n)
,空间复杂度为O(logn)
。这道题要注意的地方就是要记得更新以结点为父节点的树的最大值和最小值。以便递归返回时给上一个调用进行推断。
三. 演示样例代码
#include <iostream>
struct TreeNode
{
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
class Solution
{
private:
bool isValidBST(TreeNode* root, int &MinValue, int &MaxValue)
{
if (!root)
{
return true;
}
if (root->left)
{
if (root->val <= root->left->val)
{
return false;
}
int LeftMinValue = 0;
int LeftMaxValue = 0;
if (!isValidBST(root->left, LeftMinValue, LeftMaxValue))
{
return false;
}
else
{
MinValue = LeftMinValue;
if (LeftMaxValue != LeftMinValue)
{
if (root->val <= LeftMaxValue)
{
return false;
}
}
}
}
else
{
MinValue = root->val;
}
if (root->right)
{
if (root->val >= root->right->val)
{
return false;
}
int RightMinValue = 0;
int RightMaxValue = 0;
if (!isValidBST(root->right, RightMinValue, RightMaxValue))
{
return false;
}
else
{
MaxValue = RightMaxValue;
if (RightMaxValue != RightMinValue)
{
if (root->val >= RightMinValue)
{
return false;
}
}
}
}
else
{
MaxValue = root->val;
}
return true;
}
public:
bool isValidBST(TreeNode* root)
{
int MinValue = 0;
int MaxValue = 0;
bool IsLeaf = true;
return isValidBST(root, MinValue, MaxValue);
}
};
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 dfs Validate Binary Search Tree
Validate Binary Search Tree Total Accepted: 23828 Total Submissions: 91943My Submissions Given a bin ...
- Java for LeetCode 098 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 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 defined as ...
- 【leetcode】Validate Binary Search Tree(middle)
Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...
- 【题解】【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 98 Validate Binary Search Tree ----- java
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 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 ...
随机推荐
- python+selenium+Jenkins构建自动化测试
环境准备 本次使用JDK:1.8.tomcat:8.5.Jenkins:2.127 安装jdk 官方下载地址 安装教程参考 安装tomcat 和jenkins tomcat官方下载地址 jenkins ...
- SpringBank 开发日志 重新设计Action调用Service的参数传递 使用泛型解决类型转换问题
之前想的比较简单,请求到达controller的时候,传给action的参数没有经过任何封装,就是一个Map.然后action再调用service的时候,传递的参数也是map @Controller ...
- 微信WebView关闭后本地cookie无法清除问题
问题背景 在微信WebView下的页面中登录后,关闭WebView返回后再次进入页面,发现登录态还存在,原因是微信不会主动清除cookie以及其他的缓存. 期望是关闭窗口后会清除cookie,重新进入 ...
- \x 开头编码的数据解码成中文
在python里,直接decode('utf-8')即可 >>> "\xE5\x85\x84\xE5\xBC\x9F\xE9\x9A\xBE\xE5\xBD\x93 \xE ...
- Codeforces 460D Little Victor and Set(看题解)
Little Victor and Set 其他都很好求, 只有k == 3的时候很难受.. 我们找到第一个不大于l的 t, 答案为 l, 3 * t, (3 * t) ^ l 感觉好像是对的, 感觉 ...
- Python 动态生成多个变量
引用自:https://blog.csdn.net/u013061183/article/details/78015673 用Python循环创建多个变量, 如创建 a1= .a2= .a3= ...
- 【Java】 剑指offer(32) 从上往下打印二叉树
本文参考自<剑指offer>一书,代码采用Java语言. 更多:<剑指Offer>Java实现合集 题目 (一)从上往下打印出二叉树的每个结点,同一层的结点按照从左到右的顺 ...
- unity pattern not found
在之前破解成功再次破解或者电脑上多个版本Unity往往会导致破解失败. 破解失败解决方法如下: 先用破解软件破解,虽然是破解失败但是还是可以在破解的目录下找到那个ulf文件. 这时候直接打开unity ...
- maven环境的配置,如果jar包下载不下来,其他配置无错误的话,极有可能是网速的缘故
1首先下载apach maven 2配置maven环境变量 m2_home maven的源文件的路径 path变量后跟 %m2_home%\bin 3cmd 控制台运行mvn -version 查看 ...
- 转载收藏用<meta name="ROBOTS"
SEO优化meta标签 name="robots" content="index,follow,noodp,noydir"解释 (2012-10-11 10:33:08)转载 SEO优化meta标 ...