My c++ 13ms solution


  • 0
    S

    First get the inorder traversal of the tree, then check the inorder traversal result, if the traversal result is ascending than true else false

    class Solution {
    public:
       bool isValidBST(TreeNode* root) {
           vector<int> vals;
           if(!root) return true;
           helper(root, vals);
           for(int i=0;i<vals.size()-1;i++)
               if(vals[i+1]<=vals[i]) return false;
           return true;
       }
       void helper(TreeNode* root, vector<int>& vals){
           if(!root) return;
           helper(root->left, vals);
           vals.push_back(root->val);
           helper(root->right, vals);
       }
       
    };
    

Log in to reply
 

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