题意:判断一棵树是否是二叉搜索树;
思路:中序遍历看是否是有序的
class Solution
{
public:
bool isValidBST(TreeNode* root)
{
int pre = INT_MIN;
bool res = true;
inorder(pre, root, res);
return res;
}
void inorder(int& pre, TreeNode* root, bool &res)
{
if (root)
{
inorder(pre, root->left, res);
if (root->val<=pre)
{
res = false;
return;
}
pre = root->val;
inorder(pre, root->right, res);
}
}
};