leetcode Recover Binary Search Tree

来源:互联网 发布:球球大作战补领号软件 编辑:程序博客网 时间:2024/05/14 05:28

Two elements of a binary search tree (BST) are swapped by mistake.

Recover the tree without changing its structure.

Note:
A solution using O(n) space is pretty straight forward. Could you devise a constant space solution?

confused what "{1,#,2,3}" means? > read more on how binary tree is serialized on OJ.


Give an inorder traversal for the binary tree. 

Find the first node res1, which is larger than its successor. 

Find the first node p, which is larger than res1->val. So the predecessor of p is the second index we wanna find. 

However, in the recursive program, there are some mistake, here is wrong code: 

/** * Definition for binary tree * struct TreeNode { *     int val; *     TreeNode *left; *     TreeNode *right; *     TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution { public:   TreeNode *last = NULL, *res1 = NULL, *res2 = NULL;  void inorder(TreeNode* root) {    if (root == NULL)      return;    if (res1 && res2)      return;    inorder(root->left);        if (last != NULL && res1 == NULL && last->val > root->val)       res1 = last;    else if (last != NULL && res1 != NULL && root->val > res1->val) {      res2 = last;      swap(res1->val, last->val);      return;      }    last = root;        inorder(root->right);  }    void recoverTree(TreeNode *root) {    // Note: The Solution object is instantiated only once and is reused by each test case.    last = res1 = res2 = NULL;    inorder(root);    if (res2 == NULL)      swap(res1->val, last->val);    //if (res1 && res2)    //  swap(res1->val, res2->val);      }};

The wrong case is :

Input:{10,5,15,0,8,13,20,2,-5,6,9,12,14,18,25}Output:{10,5,15,0,8,13,20,2,-5,6,9,12,14,18,25}Expected:{10,5,15,0,8,13,20,-5,2,6,9,12,14,18,25}

The reason is that the branch else if is executed more than once although returning. Take the WA case for example, after visiting 5, 8 is returned. But 10 is visited afterwards: TreeNode* last hasn't been updated, so swap the second time. The right code is: 

/** * Definition for binary tree * struct TreeNode { *     int val; *     TreeNode *left; *     TreeNode *right; *     TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution { public:   TreeNode *last = NULL, *res1 = NULL, *res2 = NULL;  void inorder(TreeNode* root) {    if (root == NULL)      return;    if (res1 && res2)      return;    inorder(root->left);        if (last != NULL && res1 == NULL && last->val > root->val)       res1 = last;    else if (last != NULL && res1 != NULL && res2 ==NULL && root->val > res1->val) {      res2 = last;      swap(res1->val, last->val);      return;      }    last = root;        inorder(root->right);  }    void recoverTree(TreeNode *root) {    // Note: The Solution object is instantiated only once and is reused by each test case.    last = res1 = res2 = NULL;    inorder(root);    if (res2 == NULL)      swap(res1->val, last->val);    //if (res1 && res2)    //  swap(res1->val, res2->val);      }};


原创粉丝点击