99. Recover Binary Search Tre

Two elements of a binary search tree (BST) are 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?

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

解法:来自leetcode 150题集

O(n)的解法,开一个指针数组,中序遍历,将节点指针一次存放在数组中,

然后寻找两处逆向的位置,先从前往后找第一个逆序的位置,然后从后往前寻找第二个逆序的位置,交换两个指针的值,

递归/非递归中序遍历一般需要用到栈,空间也是O(n)的,可以利用morris中序遍历的方式

=====

code:

/**
 * 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){
        pair<TreeNode *,TreeNode *> broken;
        TreeNode *curr = root;
        TreeNode *prev = nullptr;
        broken.first = broken.second = nullptr;

        while(curr!=nullptr){
            if(curr->left==nullptr){
                detect(broken,prev,curr);
                prev = curr;
                curr = curr->right;
            }else{
                auto node = curr->left;
                ///prev = curr->left;
                while(node->right != nullptr && node->right!=curr){
                    node = node->right;
                }

                ///find predecessor
                if(node->right==nullptr){
                    node->right = curr;
                    curr = curr->left;
                }else{
                    node->right = nullptr;
                    detect(broken,prev,curr);
                    prev = curr;
                    curr = curr->right;
                }
            }///if-else
        }///while

        swap(broken.first->val,broken.second->val);
    }
    void detect(pair<TreeNode *,TreeNode *> &broken,TreeNode *prev,
        TreeNode *curr){
        if(prev!=nullptr && prev->val > curr->val){
            if(broken.first == nullptr) broken.first = prev;
            broken.second = curr;
        }
    }
};
时间: 2024-11-07 02:04:18

99. Recover Binary Search Tre的相关文章

99. Recover Binary Search Tree

two elements of a binary search tree (BST) are 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? 解题思路:本题主要是考查了Morris Traversal

leetcode 99 Recover Binary Search Tree ----- java

Two elements of a binary search tree (BST) are 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? 给定一个排序二叉树,然后任意交换了其中两个节点,要求在使用

LeetCode OJ 99. Recover Binary Search Tree

Two elements of a binary search tree (BST) are 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? Subscribe to see which compan

[leedcode 99] Recover Binary Search Tree

Two elements of a binary search tree (BST) are 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? /** * Definition for a binary

99. Recover Binary Search Tree -- 找到二叉排序树中交换过位置的两个节点

Two elements of a binary search tree (BST) are 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? /** * Definition for a binary

[LeetCode] 99. Recover Binary Search Tree 复原二叉搜索树

Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing its structure. Example 1: Input: [1,3,null,null,2]   1   /  3     2 Output: [3,1,null,null,2]   3   /  1     2 Example 2: Input: [3,1,4,null,null,2]

19.3.2 [LeetCode 99] Recover Binary Search Tree

Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing its structure. Example 1: Input: [1,3,null,null,2]   1   /  3     2 Output: [3,1,null,null,2]   3   /  1     2 Example 2: Input: [3,1,4,null,null,2]

leetcode[99] Recover Binary Search Tree

题目:二叉树中有两个节点对换了值,恢复他们. 思路:因为中序遍历是有序的,如果中序遍历后的数组出现乱序,说明就是交换的.从前往后,第一次乱序的第一个,后最后一次乱序的后一个,然后把这两个值对换就好了. 想了个非常挫的办法. 先中序遍历Binary Tree Inorder Traversal,然后在数组中找到两个值,然后再中序遍历找到那两个值,交换一下.虽然AC了,但不忍直视啊. /** * Definition for binary tree * struct TreeNode { * int

LeetCode: Recover Binary Search Tree

LeetCode: Recover Binary Search Tree Two elements of a binary search tree (BST) are 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 s