class Solution {
public:
bool isValidBST(TreeNode* root) {
if(root==NULL){
return true;
}
vector<int> res=inOrderTraverse(root);
for(auto it=res.begin();it<res.end()-1;it++){
if(*it>=*(it+1)){
return false;
}
}
return true;
}
vector<int> inOrderTraverse(TreeNode *root){
vector<int> res;
if(root==NULL){
return res;
}
stack<TreeNode *> s;
s.push(root);
int flag=1;
while(!s.empty()){
if(s.top()->left!=NULL&&flag){
s.push(s.top()->left);
}
else{
TreeNode *t=s.top();
s.pop();
flag=0;
res.push_back(t->val);
if(t->right!=NULL){
s.push(t->right);
flag=1;
}
}
}
return res;
}
};
LeetCode - 98 Validate Binary Search Tree
最新推荐文章于 2021-03-26 19:16:30 发布