My c++ 13ms solution

  • 0

    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 {
       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);
           helper(root->right, vals);

Log in to reply

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