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 ...
随机推荐
- <记录> PHP 缓存区ob
介绍: ob是output buffering的简称,输出缓冲区,缓冲区是通过php.ini中的output_buffering变量控制的.其默认值是off,可以设置为on来打开buffer.打来bu ...
- 使用子查询创建表(oracle)
转自:https://blog.csdn.net/lxh123456789asd/article/details/81164321 语句: CREATE TABLE tablename[(column ...
- ABAP-计算器-动态表达式
data:lv_value type string. call function 'EVAL_FORMULA' exporting formula = '90 <= 90' program = ...
- C#与.NET概述
.NET Framework是一个支持生成和运行下一代应用程序和web服务的集成在Windows中的组件. 关键组建为CLR和FCL. 为其运行的应用程序提供各种服务的托管执行环境,简化的开发和部署以 ...
- 2018SDIBT_国庆个人第一场
A - Turn the Rectangles CodeForces - 1008B There are nn rectangles in a row. You can either turn eac ...
- python list元素为dict时的排序
# 简单的dict lst = [('d', 2), ('a', 4), ('b', 3), ('c', 2)] # 按照value排序 lst.sort(key=lambda k: k[1]) pr ...
- 如何遍历Set对象
对 set 的遍历 1.迭代遍历: Set<String> set = new HashSet<String>(); Iterator<String> it = s ...
- KVM虚拟化技术(二)KVM介绍
KVM:Kernel Virtual Machine KVM是基于虚拟化扩展的x86硬件,是Linux完全原生的全虚拟化解决方案.部分半虚拟化支持,主要是通过半虚拟网络驱动程序的形式用于Linux和W ...
- 使用DOM的方法获取所有li元素,然后使用jQuery()构造函数把它封装为jQuery对象
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 18.4 运行脚本 sudo ./launcher.sh 必须先给他权限 才能使用
我们发现我们每次启动服务都要打一大堆命令,ng build --watch ,nodemon server.js ,等等 凡是重复的工作,我们要可以代码完成 要想运行它呢 你必须将你运行的上述(脚本中 ...