98. Validate Binary Search Tree solution


  • 0
    A

    type my code below. the binary search tree is which the inorderTraversal is sorted in order.
    class Solution {
    public:
    vector<int> ans;
    int flag = 1;
    vector<int> inorderTraversal(TreeNode* root) {
    if(root){
    inorderTraversal(root->left);
    ans.push_back(root->val);
    inorderTraversal(root->right);
    }
    return ans;
    }
    bool isValidBST(TreeNode* root) {
    flag = 1;
    inorderTraversal(root);
    for(int i =1 ;i< ans.size();i++){
    if(ans[i]<=ans[i-1]){
    flag = 0;
    break;
    }
    }
    if (flag ==1 )
    return true;
    else
    return false;
    }
    };


  • 0
    S
    This post is deleted!

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.