[c++] fast solution


  • 0
    9

    Inorder +vector Given a binary tree, determine if it is ASC array

    class Solution {
        vector<int> tv;
        bool result=true;
    public:
        bool isValidBST(TreeNode* root) {
            if(!root||(!root->left&&!root->right))   return true;
            Inorder(root);
            return result;
        }
        void Inorder(TreeNode* root)
        {
            if(!root);
            else
            {
                Inorder(root->left);
                tv.push_back(root->val);
                if(tv.size()>1&&tv[tv.size()-1]<=tv[tv.size()-2]) result=false;   //in order to save time;
                else Inorder(root->right);
            }
        }
    };

Log in to reply
 

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