c++ using inorder traversal


  • 0
    F
    public:
        bool isValidBST(TreeNode* root) {
            //in-order-travsersal
            if(!root)   return true;
            vector<int> inorder;
            help(root, inorder);
            for(int i=1; i < inorder.size();++i){
                if(inorder[i] <= inorder[i-1])   return false;
            }
            return true;
        }
        
        void help(TreeNode* root, vector<int> &temp){
            if(!root)   return;
            help(root->left,temp);
            temp.push_back(root->val);
            help(root->right,temp);
        }
    };
    

Log in to reply
 

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