bool findSmall(TreeNode *root, TreeNode **pre) { if (root == NULL) return false; bool find = findSmall(root->left, pre); if (find) return true; if (root->val < (*pre)->val) { return true; } *pre = root; return findSmall(root->right, pre); }
bool findSmall(TreeNode *root, int &pre) { if (root == NULL) return false; bool find = findSmall(root->left, pre); if (find) return true; if (root->val < pre) { return true; } small = root; pre = root->val; return findSmall(root->right, pre); }
int main() { int testArr[8] = { 1, 2, 4, 5, 10, 100, 2, -22 }; // prints smallest number in array std::cout << findSmall(testArr, 8) << std::endl; }
void recoverTree(TreeNode *root) { // Start typing your C/C++ solution below // DO NOT write int main() function TreeNode *p = new TreeNode(INT_MIN); TreeNode *l = new TreeNode(INT_MAX); findSmall(root, &p); findBig(root, &l); int temp = p->val; p->val = l->val; l->val = temp; }
void recoverTree(TreeNode *root) { // Start typing your C/C++ solution below // DO NOT write int main() function int pre = INT_MIN; int lat = INT_MAX; findSmall(root, pre); findBig(root, lat); int temp = small->val; small->val = big->val; big->val = temp; }