98. Validate Binary Search Tree - CPP - Solution


  • 1
    Y
    /**
     * Definition for a binary tree node.
     * struct TreeNode {
     *     int val;
     *     TreeNode *left;
     *     TreeNode *right;
     *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
     * };
     */
    class Solution {
    public:
        bool isValidBST(TreeNode* root) {
            bool result = true;
            vector<int> v;
            this->inOrderTraverse(root, v);
            for (size_t i = 0; i < v.size(); ++i) {
                if (i + 1 < v.size()) {
                    if (v[i + 1] <= v[i]) {
                        result = false;
                        break;
                    }
                }
            }
            return result;
        }
    private:
        void inOrderTraverse(TreeNode* root, vector<int>& v) {
            if (root) {
                inOrderTraverse(root->left, v);
                v.push_back(root->val);
                inOrderTraverse(root->right, v);
            }
        }
    };

Log in to reply
 

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