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,一种根据线索二叉树思想的时间复杂度为O(n),空间复杂度为O(1)的中序遍历方式。

在博客http://www.cnblogs.com/AnnieKim/archive/2013/06/15/MorrisTraversal.html里,原作者对Morris Traversal说的很明白。此题在此基础上只要比较相邻两个数的大小,就可以确定哪两个是打乱序的了。而Morris 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:
    void recoverTree(TreeNode* root) {
        TreeNode *pre=NULL,*cur=root;
        TreeNode *first=NULL,*second=NULL,*temp=NULL;
        while(cur!=NULL){
            if(cur->left==NULL){
                //cur->val;
                if(temp!=NULL&&temp->val>cur->val){
                    if(!first)first=temp;
                    second=cur;
                }
                temp=cur;
                cur=cur->right;
            }
            else {
                pre=cur->left;
                while(pre->right!=NULL&&pre->right!=cur)
                pre=pre->right;
                //exist threaded tree
                if(pre->right==cur){
                    //print cur->val;
                    if(temp!=NULL&&temp->val>cur->val){
                        if(!first)first=temp;
                        second=cur;
                    }
                    temp=cur;
                    pre->right=NULL;
                    cur=cur->right;
                }
                //creating backend poinner to the current point
                else{
                    pre->right=cur;
                    cur=cur->left;
                }
            }
        }
        swap(first->val,second->val);
    }
};
时间: 2024-10-11 10:59:24

99. Recover Binary Search Tree的相关文章

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

【leetcode】Recover Binary Search Tree

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? 中序