LeetCode "Validate Binary Search Tree"

This is to test your knowledge on BST and its traversal. 

Flatting BST into an array using in-order, and check that array. It is that simple:

class Solution {
public:
    void serial(TreeNode *root, vector<int> &vec)
    {
        if (!root) return;

        if (root->left)        serial(root->left, vec);
        vec.push_back(root->val);
        if (root->right)    serial(root->right, vec);
    }
    bool isValidBST(TreeNode *root) {
        if (!root) return true;

        vector<int> arr;
        serial(root, arr);

        for (int i = 0; i < arr.size() - 1; i ++)
        {
            if (arr[i + 1] <= arr[i]) return false;
        }
        return true;
    }
};
原文地址:https://www.cnblogs.com/tonix/p/3851260.html