[Leetcode] [JAVA] Recover Binary Search Tree (Morris inorder traversal)

Source: Internet
Author: User

Elements of a binary search tree (BST) is 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?

With O (n) space, the problem node can be found directly through the sequential traversal.

If it is an O (1) space, it is basically only in-situ operation.

Here is an introduction to Morris Inorder traversal. can achieve:

1. If the current node has a left dial hand tree, locate the right-most node of Zuozi and point its right pointer to the current node. The current node is transferred to its left node.

2. If the right-hand node of Zuozi has pointed to the current node (previously traversed Zuozi), the right pointer to the right-most node is null, the current node is output, and the current node is transferred to its right node.

3. If the current node has no left subtree, output the current node directly and transfer it to the right node.

Write the code as follows:

1      Public voidrecovertree (TreeNode root) {2TreeNode cur =Root;3          while(cur!=NULL) {4             if(cur.left!=NULL) {5TreeNode temp =Cur.left;6                  while(temp.right!=NULL&& temp.right!=cur)7temp =Temp.right;8                 if(temp.right==NULL) {9Temp.right =cur;TenCur =Cur.left; One                 } A                 Else { -temp.right=NULL; -                     //Print theCur=Cur.right; -                 } -             } -             Else { +                 //Print -Cur=Cur.right +             } A         } at     } -}

In combination with this problem, just add the code to the print location.

There is only one error, and there are two possible scenarios of traversal:

1. Found that the original node value is larger than the current node value, for example: (1, 4, 3, 7, 9) This time only to swap the two node values.

2. Two times the original node value is found to be larger than the current node value, for example: (1, 9, 4, 5, 3, 10) The first value of the original node and the second current node value need to be swapped at this time.

Using two TreeNode Wrong1, Wrong2 records the two error nodes, if it is the first time that the original node is larger than the current value of the error, then wrong1 to the original node, wrong2 to the current node. If one is found again, only the wrong2 is changed.

Note the relationship between the previous node pre and the current node cur, and the pre is only set to cur until cur is about to move to the right node. (Because cur to the left node is the first time to traverse the left subtree, only for the connection, the second traversal is output)

The complete code is as follows:

1      Public voidrecovertree (TreeNode root) {2TreeNode cur =Root;3TreeNode pre =NULL;4TreeNode wrong1 =NULL;5TreeNode wrong2 =NULL;6          while(cur!=NULL) {7             if(cur.left!=NULL) {8TreeNode temp =Cur.left;9                  while(temp.right!=NULL&& temp.right!=cur)Tentemp =Temp.right; One                 if(temp.right==NULL) { ATemp.right =cur; -Cur =Cur.left; -                 } the                 Else { -temp.right=NULL; -                     //Print -                     if(pre!=NULL&& pre.val>cur.val) { +                         if(wrong1==NULL) -wrong1=Pre; +Wrong2 =cur; A                     } atPre =cur; -Cur=Cur.right; -                 } -             } -             Else { -                 //Print in                 if(pre!=NULL&& pre.val>cur.val) { -                     if(wrong1==NULL) towrong1=Pre; +Wrong2 =cur; -                 } thePre =cur; *Cur=Cur.right; $             }Panax Notoginseng         } -         intt =Wrong1.val; theWrong1.val =Wrong2.val; +Wrong2.val =T; A}

[Leetcode] [JAVA] Recover Binary Search Tree (Morris inorder traversal)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.