98. Validate Binary Search Tree (Tree; DFS)
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.
Example 1:
2
/ \
1 3
Binary tree [2,1,3], return true.
Example 2:
1
/ \
2 3
Binary tree [1,2,3], return false.
思路:由于Binary Tree的中序遍历结果是正序,所以可以检查中序遍历的结果是否递增
/**
* 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) {
if(root==NULL) return true;
TreeNode* pre = NULL; //we don't need to save all nodes, only a previous node is enough to know whether it's an increase sequence
return inOrderTraverse(root,pre);
} bool inOrderTraverse(TreeNode* root, TreeNode* &pre){ //important to use &, otherwise new object will use a new address and the result won't bring back to caller
//visit left child
if(root->left)
if(!inOrderTraverse(root->left,pre))
return false; //visit root
if(pre==NULL) pre = new TreeNode(root->val);
else if(root->val > pre->val) pre->val = root->val;
else return false; //visit right child
if(root->right) return inOrderTraverse(root->right, pre);
else return true;
}
};
98. Validate Binary Search Tree (Tree; DFS)的更多相关文章
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
- 【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 ----- 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] 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 ...
- 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 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 ...
随机推荐
- 解决 shopnc b2b2c 版权问题 修改路经ULR及目录文件夹思路及教程
相信各位使用过NC的朋友,多多少少收到过律师函,把一堆人吓尿了,原因你使用了盗版,大哥都要吃饭可以理解#网络那么大,他怎么能快速定位到您的,原因很简单 搜索引擎,NC在开发中定义了URL路由规则,在百 ...
- svn下载地址
SVN svn服务器端下载: https://www.visualsvn.com/server/download/ svn eclipse插件地址(new soft install): http:// ...
- RACSignal常见用法
RACSignal 两种用法, 1:异步操作,一般创建signal的时候写逻辑,然后通过subscribeNext拿到异步执行的结果 2:监听的属性的变化,及时给出回应,一般赋值的时候用RACObse ...
- BlockingQueue之DelayQueue的学习使用
DelayQueue 是一中阻塞队列,需要实现接口Delayed定义的方法.做下使用记录和心得吧, @Datapublic class DelayQueueExample implements Del ...
- lientDataset的Delta与XML相互转换
一个ClientDataset的Delta与XML相互转换的文章:大家都知道TClientDataSet的Delta属性保存数据集的变化,但是Delta是OleVariant类型的属性,这样如果用De ...
- python 停止线程
Python没有提供方法去结束一个线程,无法给它发送信号,无法调整它的调度,也无法执行其他高级操作. 如果需要终止线程,需要通过编程让这个线程在某个特定点轮询来退出.但是如果一个线程一直阻塞在一个 I ...
- spring Boot 上传文件,10天后,不能上传的bug
起因 公司研发人员 部署服务在阿里云 ecs 服务器; 上传文件过1周左右文件自动丢失; 排查思路: (1).查询tomcat 启动日志出现如下信息: java.io.IOException: The ...
- c#自定义类型的转换方式operator,以及implicit(隐式)和explicit (显示)声明
https://docs.microsoft.com/zh-cn/dotnet/csharp/language-reference/keywords/explicit https://docs.mic ...
- U盘无法访问
U盘无法访问 方法/步骤 首先,Win+R,打开“运行”窗口. 在打开的运行窗口中,输入cmd回车 这时会打开这样的一个窗口 这时输入chkdsk g: /f 需要说明的是,g这个 ...
- ReactiveX 学习笔记(2)创建数据流
操作符(Operators) Rx 的操作符能够操作(创建/转换/组合) Observable. Creating Observables 本文主题为创建/生成 Observable 的操作符. 这里 ...