https://leetcode.com/problems/validate-binary-search-tree


Runtime: 568 ms, faster than 5.88% of C++ online submissions for Validate Binary Search Tree.
Memory Usage: 583.6 MB, less than 7.32% of C++ online submissions for Validate Binary Search Tree.


```cpp
#include <bits/stdc++.h>

using namespace std;

//struct TreeNode {
//    int val;
//    TreeNode *left;
//    TreeNode *right;
//    TreeNode() : val(0), left(nullptr), right(nullptr) {}
//    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
//    TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
//};

class Solution {
public:
    vector<int> get_all_values_of_a_subtree(TreeNode* node) {
      vector<int> l;
      if (node != nullptr) {
        l.push_back(node->val);
        if (node->left != nullptr) {
          l.push_back(node->left->val);
        }
        if (node->right != nullptr) {
          l.push_back(node->right->val);
        }
        vector<int> left = get_all_values_of_a_subtree(node->left);
        l.insert(l.end(), left.begin(), left.end());
        vector<int> right = get_all_values_of_a_subtree(node->right);
        l.insert(l.end(), right.begin(), right.end());
        return l;
      }
      return l;
    }

    bool verify_each_subtree(TreeNode* node) {
      if (node != nullptr) {
        auto left_list = get_all_values_of_a_subtree(node->left);
        auto right_list = get_all_values_of_a_subtree(node->right);

        if (left_list.size() > 0) {
          if (*max_element(left_list.begin(), left_list.end()) < node->val) {
            // normal
          } else {
            return true;
          }
        }

        if (right_list.size() > 0) {
          if (*min_element(right_list.begin(), right_list.end()) > node->val) {
            // normal
          } else {
            return true;
          }
        }

        return verify_each_subtree(node->left) || verify_each_subtree(node->right);
      }

      return false;
    }

    bool isValidBST(TreeNode* root) {
      //7:19
      return !verify_each_subtree(root);
      //7:29
    }
};
```