leetcode99. Restore binary search tree/in-order traversal

Problem: Two nodes in the binary search tree are incorrectly swapped.

Please restore this tree without changing its structure.

Example 1:

输入: [1,3,null,null,2]

   1
  /
 3
  \
   2

输出: [3,1,null,null,2]

   3
  /
 1
  \
   2

Example 2:

输入: [3,1,4,null,null,2]

  3
 / \
1   4
   /
  2

输出: [2,1,4,null,null,3]

  2
 / \
1   4
   /
  3

Advanced:

  • The solution using O(n) space complexity is easy to implement.
  • Can you think of a solution that uses only constant space?

Source: LeetCode
Link: https://leetcode-cn.com/problems/recover-binary-search-tree The
copyright is owned by LeetCode . For commercial reprints, please contact the official authorization. For non-commercial reprints, please indicate the source.

Basic idea 1: In-order traversal

The result of the in-order traversal of the binary search tree must be an ascending sequence, so the sequence in the middle-order traversal in this question becomes an ascending sequence after exchanging two elements.

  • First, perform an in-order traversal of the binary tree, and save the result sequence of the in-order traversal
  • Find the element pre before the first reversed element in the result sequence
  • Search from the element pre to the back. When an element temp larger than the element is found, the element post before temp is the element that needs to be exchanged
  • Exchange the two elements of pre and post

The space complexity is O(n).
Define an array to save the order of in-order traversal

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
    
    
public:
    vector<TreeNode*> vec;
    void recoverTree(TreeNode* root) {
    
    
        inorder(root);
        int pre = 0, i;
        for(i = 1; i < vec.size(); ++i){
    
    
            if(vec[i]->val > vec[pre]->val)
                ++pre;
            else
                break;
        }
        
        for(; i < vec.size(); ++i){
    
    
            if(vec[i]->val > vec[pre]-> val)
                break;
        }
        --i;
        int t = vec[i]->val;
        vec[i]->val = vec[pre]->val;
        vec[pre]->val = t;
        
    }
    void inorder(TreeNode* root){
    
    
        if(root == NULL)
            return;
        inorder(root->left);
        vec.push_back(root);
        inorder(root->right);
    }
};

The space complexity is O(1):
define two variables to save the two nodes in reverse order

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
    
    
public:
    
    void recoverTree(TreeNode* root) {
    
    
        TreeNode* pre1 = NULL, *pre2 = NULL;
        inorder(root, pre1, pre2);
        int t = pre1->val;
        pre1->val = pre2->val;
        pre2->val = t;
    }
    void inorder(TreeNode* root, TreeNode* &pre1, TreeNode* &pre2){
    
    
        if(root == NULL)
            return;
        inorder(root->left, pre1, pre2);
        if(pre1 == NULL){
    
    //root是根节点的情况
            pre1 = root;
        }
        else{
    
    
            if(root->val > pre1->val && (!pre2 || root->val < pre2->val)){
    
    
                pre1 = root;
            }
            else if(pre2 == NULL){
    
    //此时找到了第一个需要交换的节点,保存在pre2中
                pre2 = pre1;
                pre1 = root;
            }
            if((root->val < pre1->val) && (pre2) && (pre2->val > root->val)){
    
    //此时找到了第二个需要交换的节点保存在pre1中
                pre1 = root;
            }
        }
        inorder(root->right, pre1, pre2);
    }
};

Guess you like

Origin blog.csdn.net/qq_31672701/article/details/107880419