问题遇到的现象和发生背景
https://leetcode-cn.com/problems/validate-binary-search-tree/
问题相关代码,请勿粘贴截图
class Solution {
vector<int> result;
public:
void morris(TreeNode* root){
if(root == nullptr) return;
auto cur = root;
TreeNode* tmp = nullptr;
while(cur!=nullptr){
tmp = cur->left;
if(tmp){
while(tmp->right!=nullptr&&tmp->right!=cur)
tmp = tmp->right;
if(tmp->right == nullptr){
tmp->right = cur;
cur = cur->left;
continue;
}
else
tmp->right == nullptr;
}
result.push_back(cur->val);
cur = cur->right;
}
}
public:
bool isValidBST(TreeNode* root) {
morris(root);
for(int i = 1;i<result.size();i++){
if(result[i] <= result[i-1]){
return false;
}
}
return true;
}
};