当前位置:网站首页>98. Verify the binary search tree ●●
98. Verify the binary search tree ●●
2022-07-05 23:24:00 【chenyfan_】
98. Verify binary search tree ●●
describe
Give you the root node of a binary tree root , Determine whether it is an effective binary search tree .
It works The binary search tree is defined as follows :
- The left subtree of the node contains only Less than Number of current nodes .
- The right subtree of the node contains only Greater than Number of current nodes .
- All left and right subtrees themselves must also be binary search trees .
Example
Input :root = [5,1,4,null,null,3,6]
Output :false
explain : The value of the root node is 5 , But the value of the right child node is 4 .
Answer key
nature : Middle order traversal , The value of the output binary search tree node is an ordered sequence .
Therefore, you can traverse in order and record all elements with an array , Then traverse the array to judge .
But it can also overall situation Define a Value to be compared , Directly in the middle order traversal process Comparative judgment And constantly update the value to be compared , It should be noted that :
You cannot simply compare that the left node is smaller than the middle node , The right node is larger than the middle node , What we want to compare is All nodes of the left subtree Smaller than the intermediate node , All nodes in the right subtree Larger than the intermediate node , Therefore, compare with the previous nodes .

1. In the sequence traversal recursive
- Initialize the value to be compared to long long Type min , Deal with the situation that there is an integer minimum in the question ;
class Solution {
public:
long long preValue = LONG_MIN; // The previous number before initialization is the long minimum
bool isValidBST(TreeNode* root) {
if(!root) return true;
bool left = isValidBST(root->left); // Left
if(root->val <= preValue){
// in
return false; // When traversing the middle order, compare with the previous number to judge the order
}else{
preValue = root->val;
}
bool right = isValidBST(root->right); // Right
return left && right; // Effectiveness of left and right subtrees
}
};
- Or define the value to be compared as the node type
class Solution {
public:
TreeNode* preNode = nullptr; // Nodes to be compared
bool isValidBST(TreeNode* root) {
if(!root) return true;
bool left = isValidBST(root->left); // Left
if(preNode != nullptr && root->val <= preNode->val){
// in
return false; // Invalid , sign out
}else{
preNode = root; // When traversing the middle order, compare with the previous number to judge the order
}
bool right = isValidBST(root->right); // Right
return left && right; // Effectiveness of left and right subtrees
}
};
2. In the sequence traversal iteration
class Solution {
public:
bool isValidBST(TreeNode* root) {
stack<TreeNode*> st;
TreeNode* preNode = nullptr; // Nodes to be compared
TreeNode* curr = root;
while(curr != nullptr || !st.empty()){
if(curr){
st.push(curr); // Nodes on the stack
curr = curr->left; // The node pointer points to the left child
}else{
// Traverse all left children
curr = st.top();
st.pop(); // in
if(preNode && curr->val <= preNode->val) return false;
preNode = curr;
curr = curr->right; // The node pointer points to the right child
}
}
return true;
}
};
- utilize Null pointer marker Unified iterative writing of
class Solution {
public:
bool isValidBST(TreeNode* root) {
stack<TreeNode*> st;
TreeNode* curr = nullptr;
TreeNode* preNode = nullptr; // The former element , Nodes to be compared
st.push(root);
while(!st.empty()){
curr = st.top();
st.pop(); // Visit children who are not at the top of the empty stack
if(curr != nullptr){
// The stack order is : Right - in -null- Left ; The stack order is : Left -null- in - Right
if(curr->right) st.push(curr->right);
st.push(curr);
st.push(nullptr);
if(curr->left) st.push(curr->left);
}else{
curr = st.top(); // null The following elements indicate that you have visited , At this point, you need to pop up and take out the element value
st.pop();
if(preNode != nullptr && curr->val <= preNode->val) return false;
preNode = curr;
}
}
return true;
}
};
边栏推荐
- Three.js-01 入门
- The maximum happiness of the party
- 2:第一章:认识JVM规范1:JVM简介;
- (4) UART application design and simulation verification 2 - RX module design (stateless machine)
- 第十七周作业
- Realize reverse proxy client IP transparent transmission
- The method and principle of viewing the last modification time of the web page
- 芯源&立创EDA训练营——无刷电机驱动
- TVS管和ESD管的技术指标和选型指南-嘉立创推荐
- Leecode learning notes
猜你喜欢

LeetCode102. Sequence traversal of binary tree (output by layer and unified output)
![[original] what is the core of programmer team management?](/img/11/d4b9929e8aadcaee019f656cb3b9fb.png)
[original] what is the core of programmer team management?

Dynamic memory management (malloc/calloc/realloc)

Three. JS VR house viewing

YML configuration, binding and injection, verification, unit of bean

2: Chapter 1: understanding JVM specification 1: introduction to JVM;

The PNG image is normal when LabVIEW is opened, and the full black image is obtained when Photoshop is opened

开关电源Buck电路CCM及DCM工作模式

Go语言实现原理——Map实现原理

Alibaba Tianchi SQL training camp task4 learning notes
随机推荐
Hainan Nuanshen tea recruits warmhearted people: recruitment of the product experience recommender of Nuanshen multi bubble honey orchid single cluster
(4)UART應用設計及仿真驗證2 —— TX模塊設計(無狀態機)
grafana工具界面显示报错influxDB Error
Negative sampling
2022 G3 boiler water treatment simulation examination and G3 boiler water treatment simulation examination question bank
Go language implementation principle -- lock implementation principle
并查集实践
3D reconstruction of point cloud
3:第一章:认识JVM规范2:JVM规范,简介;
npm ELECTRON_ Mirror is set as domestic source (npmmirror China mirror)
ORB_ SLAM2/3
It is proved that POJ 1014 module is optimized and pruned, and some recursion is wrong
Debian 10 installation configuration
February 13, 2022 -5- maximum depth of binary tree
yate. conf
TypeError: this. getOptions is not a function
Introduction to JVM
Media query: importing resources
Multi camera stereo calibration
MySQL (2) -- simple query, conditional query
