[Leetcode] Recover Binary Search Tree

Source: Internet
Author: User
Tags tree serialization

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?

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


OJ ' s Binary Tree serialization:

The serialization of a binary tree follows a level order traversal, where ' # ' signifies a path terminator where no node ex Ists below.

Here's an example:

   1  /  2   3    /   4         5
The above binary tree is serialized as "{1,2,3,#,#,4,#,#,5}".

Solution:

The main point of this problem is to think of using the recursive middle sequence traversal of the tree, because the binary search tree is a valid case, the value of the middle sequence traversal is from small to large arrangement.

When the current value is smaller than the previous value, there is an illegal node.

----------------------------------------------------------------------------

The solution is to use the middle sequence traversal to find two points of the wrong order, the last swap is good.

Because the middle of the error is two points exchanged, so is the big run front, small run back.

So in the order of convenience, meet the first sequence for the deduction of two node, the big one is certainly to be recovery one of them, to record.

The other one, to traverse the entire tree, records the last inverted node.

In simple terms, the first reverse point to record, the last reverse point to record, and finally swap a bit.

Because the inorder with the return of the solution, so in order to be able to store the two reverse points, here with the global variables, with other reference-type traversal can also be resolved.

/*** Definition for Binary tree * public class TreeNode {* Int. val; * TreeNode left; * TreeNode right; * TreeNode (int x) {val = x;} }*/ Public classSolution {TreeNode pre;    TreeNode first;    TreeNode second;  Public voidrecovertree (TreeNode root) {Pre=NULL; First=NULL; Second=NULL;        Inordertraverse (root); inttemp =First.val; First.val=Second.val; Second.val=temp; }    Private voidinordertraverse (TreeNode root) {//TODO auto-generated Method Stub        if(root==NULL)            return;        Inordertraverse (Root.left); if(pre==NULL) {Pre=Root; Pre pointer initial}Else{            if(pre.val>root.val) {                if(first==NULL) First=Pre; First reverse point second=Root; Keep looking for the last reverse point} pre=Root;    The pre pointer moves back one} inordertraverse (Root.right) at a time; }}

[Leetcode] 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.