1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24


/**
* For a binary search tree, the in-order traversal result is a sorted array
*/
bool isValidBST(TreeNode *n, TreeNode* &pre) { if (n == NULL) return true; if (!isValidBST(n->left, pre))return false; if (pre != NULL && pre->val >= n->val) return false; pre = n; if (!isValidBST(n->right, pre))return false; return true; } bool isValidBST(TreeNode *root) { TreeNode *pre = NULL; return isValidBST(root, pre); }
View Program Text


Test Status