Recover Binary Search Tree [leetcode]

Source: Internet
Author: User

On the basis of the Middle-order traversal, find the non-standard (not incremental) Tree node pairs and then exchange

First, let's look at the two sequences:

1. 1 3 2 4 => Switch 3 and 2

2. 4 3 2 1 => exchange 4 and 1

These two sequences correspond to all possibilities of the intermediate traversal sequence of the BST that does not meet the condition.

If we use an array swap to save the result of non-incrementing traversal in the middle order, the array may only be 2 or 4 in size.

Instead, we only need to exchange the content of the first and last tree node pairs.

    vector<TreeNode *> swap;    TreeNode * pre;    void recoverTree(TreeNode *root)     {        pre = new TreeNode(-999999);        swap = vector<TreeNode *>();        inorder(root);        if (swap.size() > 1)        {            int val = swap[0]->val;            swap[0]->val = swap[swap.size() - 1]->val;            swap[swap.size() - 1]->val = val;        }    }        void inorder(TreeNode * root)    {        if (root == NULL) return;        inorder(root->left);        if (pre->val > root->val)        {            swap.push_back(pre);            swap.push_back(root);        }        pre = root;        inorder(root->right);    }
Since only the content of the first and last Tree nodes is exchanged, we can save only the first and last nodes

If (! First) First = pre;

Last = root;

Replace

Swap. push_back (pre); Swap. push_back (Root );

The Code is as follows:

TreeNode * first, * last;    TreeNode * pre;    void recoverTree(TreeNode *root)     {        pre = new TreeNode(-999999);        first = last = NULL;        inorder(root);        if (first)        {            int val = first->val;            first->val = last->val;            last->val = val;        }    }        void inorder(TreeNode * root)    {        if (root == NULL) return;        inorder(root->left);        if (pre->val > root->val)        {            if (!first) first = pre;            last = root;        }        pre = root;        inorder(root->right);    }



Recover Binary Search Tree [leetcode]

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.