Elements of a binary search tree (BST) is swapped by mistake.
Recover the tree without changing its structure.
Example 1:
Input: [1,3,null,null,2] 1/3 2Output: [3,1,null,null,2] 3/1 2
Example 2:
Input: [3,1,4,null,null,2] 3/1 4 /2Output: [2,1,4,null,null,3] 2/1 4 /3
Follow up:
- A solution using O (n) space is pretty straight forward.
- Could you devise a constant space solution?
The 2 elements of the binary search tree are exchanged incorrectly, and the binary search tree is restored without changing the structure. Follow up: use constant space.
Java:
public class Solution {TreeNode firstelement = null; TreeNode secondelement = null; The reason for this initialization are to avoid null pointer exception in the first comparison when prevelement have not been initialized TreeNode prevelement = new TreeNode (integer.min_value); public void Recovertree (TreeNode root) {//In order traversal to find the Elements traverse (roo T); Swap the values of the nodes int temp = Firstelement.val; Firstelement.val = Secondelement.val; Secondelement.val = temp; private void traverse (TreeNode root) {if (root = null) return; Traverse (Root.left); Start of "Do some business",//If first element have not been found, assign it to prevelement (refer to 6 in th e example above) if (firstelement = = null && prevelement.val >= root.val) {firstelement = pr Evelement; }//If first element is found, assign the second element to the root (refer to 2 in the example above) if (firstelement! = null && prevelement.val >= root.val) {secondelement = root; } prevelement = root; End of "Do some business" traverse (root.right);}
Python:
# Time: O (N) # Space:o (1) class TreeNode (object): def __init__ (self, x): self.val = x self.left = None
self.right = None def __repr__ (self): if self: serial = [] queue = [self] while queue: cur = q UEUE[0] if cur: serial.append (cur.val) queue.append (cur.left) queue.append (cur.right) else: serial.append ("#") queue = queue[1:] While serial[-1] = = "#": serial.pop () return REPR (serial) else: return None
Python:
Class solution (Object): # @param root, a tree node # @return a tree node def recovertree (self, root): Retu RN Self. Morristraversal (Root) def morristraversal (self, root): If the root is none:return broken = [None, None] Pre, cur = None, root while Cur:if Cur.left is None:self.detectBroken (bro Ken, PRE, cur) Pre = cur cur = cur.right Else:node = Cur.left While Node.right and node.right! = Cur:node = Node.right if Node.right is N One:node.right =cur cur = cur.left Else:self.det Ectbroken (broken, Pre, cur) node.right = None pre = cur cur = cur . Right Broken[0].val, Broken[1].val = Broken[1].val, broken[0].val return root def detectbroken (self, bro Ken, Pre, cur): If pre and Pre.val > Cur.val:if broken[0] is none:broken[0] = Pre broken[ 1] = cur
C++:
O (n) Space Complexityclass solution {public: void Recovertree (TreeNode *root) { vector<treenode*> list ; Vector<int> Vals; Inorder (root, List, vals); Sort (Vals.begin (), Vals.end ()); for (int i = 0; i < list.size (); ++i) { list[i]->val = vals[i]; } } void Inorder (TreeNode *root, vector<treenode*> &list, vector<int> &vals) { if (!root) return;< C11/>inorder (Root->left, List, vals); List.push_back (root); Vals.push_back (root->val); Inorder (Root->right, List, vals); };
C++:
Still O (n) Space Complexityclass solution {public: TreeNode *pre; TreeNode *first; TreeNode *second; void Recovertree (TreeNode *root) { pre = NULL; first = NULL; second = NULL; Inorder (root); if (first && second) swap (first->val, second->val); } void Inorder (TreeNode *root) { if (!root) return; Inorder (root->left); if (!pre) pre = root; else { if (Pre->val > Root->val) { if (!first) first = pre; second = root; } Pre = root; } Inorder (root->right); }};
C++:
O (1) Space Complexityclass solution {public:void recovertree (TreeNode *root) {TreeNode *first = NULL, *seco nd = NULL, *parent = NULL; TreeNode *cur, *pre; cur = root; while (cur) {if (!cur->left) {if (parent && parent->val > Cur->val) { if (!first) first = parent; second = cur; } parent = cur; Cur = cur->right; } else {pre = cur->left; while (pre->right && pre->right! = cur) Pre = pre->right; if (!pre->right) {pre->right = cur; Cur = cur->left; } else {pre->right = NULL; if (Parent->val > Cur->val) {if (!first) first = parent; second = cur; } parent = cur; Cur = cur->right; }}} if (first && second) swap (first->val, second->val); }};
[Leetcode] 99. Recover Binary search Tree Restoration binary