/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
// Recursion
class Solution {
public:
bool isValidBST(TreeNode *root) {
if (!root) return true;
vector<int> vals;
LDR(root, vals); //中序遍历,把遍历的结果存到向量vals
for (int i = 0; i < vals.size() - 1; ++i) {
if (vals[i] >= vals[i + 1]) return false;
} //二叉搜索树的中序遍历是递增序列
return true;
}
void LDR(TreeNode *root, vector<int> &vals) {
if (!root) return;
LDR(root->left, vals);
vals.push_back(root->val);
LDR(root->right, vals);
}
};