Recover Binary Search Tree

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?

Analysis: http://www.cnblogs.com/yuzhangcmu/p/4208319.html

Specific ideas, or through the middle sequence traversal, but, do not need to store each node, only need to save a precursor.

such as 1,4,3,2,5,6

1. When we read 4, we find that it is a positive order, not a process.

2. But encountered 3 o'clock, found in reverse order, the 4 is saved as the first error node, 3 is the second error node

3. Go ahead and find 3,2 is in reverse order, then update the 2nd error node to 2

If this is the sequence: 1,4,3,5,6 Ibid, get two nodes in reverse order of 4 and 3.

========================================

Here, let's add, why replace the second node instead of the first one:
e.g. the correct BST is below:

The Inorder traversal is:1 3 4 6 7 8 10 13 14

Find the place which the order is wrong.
Wrong Order:1 3 8 6 7 4 10 13 14
Find:8 6
Then we find:7 4
8, 6 is the wrong sequence, but 7,4 is also the wrong sequence.
Because the sequence in front of the 8,6 is correct, the 8,6 must be in the back of the sequence exchange.
And the latter is a relatively large number, that is to say 8 must be exchanged over. and 7,4
Also should be the small number 4 is the front exchange over.

Use contradiction to prove:
Suppose: 6 is the back exchange.
Corollary: Then 8:6 is big, then 8 should be the back exchange,
So there's at least 3 wrong numbers.
And the title is 2 wrong number, the proof, only should be 8 is exchange over.
The conclusion: what we need to Exchange is: 8, 4.

1 /**2 * Definition for a binary tree node.3 * public class TreeNode {4 * int val;5 * TreeNode left;6 * TreeNode right;7 * TreeNode (int x) {val = x;}8  * }9  */Ten  Public classSolution { OneTreeNode pre =NULL; ATreeNode first =NULL; -TreeNode second =NULL; -      the      -      Public voidrecovertree (TreeNode root) { - inorder (root); -          +         //Swap the value of first and second node. -         intTMP =First.val; +First.val =Second.val; ASecond.val =tmp; at     } -      -      Public voidinorder (TreeNode root) { -         if(Root = =NULL) { -             return; -         } in          -         //inorder Traverse. to inorder (root.left); +  -         /* the Find the place which the order is wrong. * For example:1 3 4 6 7 8 $ wrong order:1 3 8 6 7 4Panax Notoginseng FIND: ___ - Then we find: ___ the 8, 6 is the wrong sequence, but 7,4 is also the wrong sequence.  + because the sequence in front of the 8,6 is correct, the 8,6 must be in the back of the sequence exchange.  A and the latter is a relatively large number, that is to say 8 must be exchanged over. and 7,4 the also should be the small number 4 is the front exchange over.  +  - use contradiction to prove: $ Suppose: 6 is the back Exchange . $ corollary: Then 8:6 is big, then 8 should be the back exchange, - so there's at least 3 wrong numbers. - and the title is 2 wrong number, the proof, only should be 8 is exchange over.  the         */ - Wuyi         //determine if the pre has been set the         if(Pre! =NULL&& pre.val >root.val) { -             if(First = =NULL) { Wu                 //first found in reverse order. -First =Pre; AboutSecond =Root; $}Else { -                 //the second time to find the reverse order, update second. -Second =Root; -             } A         } +  thePre =Root; -  $         //inorder Traverse. the inorder (root.right); the     } the}

Recover Binary Search Tree

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.