5.1.7 Recover Binary Search Tree

来源:互联网 发布:app store下载mac os 编辑:程序博客网 时间:2024/06/05 05:53

我的思路: 找出那些不满足node.val > node.left.val或者node.val < node.right.val的节点。但是问题是这样的点不一定被swap过,有可能是因为那两个swap过的节点,导致其他节点不满足BST的性质。

所以,如何判断哪两个节点被交换过呢?

不会做。思路:中序遍历BST是一个有序数组,找到逆序的位置。若两个交换节点在中序下相邻,则只存在一对逆序节点。若不相邻,则第一个交换节点是第一对逆序的前一个,第二个交换节点是第二对逆序的后一个(还没有理解为什么)。

O(n) 解法:

1 开一个指针数组,中序遍历

2 递归(实际上用了栈,O(n)?).

public class Solution {    //This code is from http://blog.csdn.net/fightforyourdream/article/details/16875941, but I don't understand    TreeNode pre = null;    TreeNode first = null;    TreeNode second = null;        public void recoverTree(TreeNode root) {        inorder(root);        if(first != null && second != null){             int tmp = first.val;            first.val = second.val;            second.val = tmp;        }    }        private void inorder(TreeNode root){        if(root == null) return;        inorder(root.left);        if(pre == null){            pre = root;        }        else{            if(pre.val > root.val){                if(first == null){                    first = pre;                }                second = root;            }            pre = root;        }        inorder(root.right);    }}

方法II: Morris Inorder Traversal, O(1)

Refer: http://www.cnblogs.com/echoht/p/3710523.html

public class Solution {    //O(1), Recover tree using Morris Inorder traversal    public void recoverTree(TreeNode root) {        //(pre1, cur1), (pre2, cur2) are the indices of the two possible reversing pairs        TreeNode pre1 = null;        TreeNode cur1 = null;        TreeNode pre2 = null;        TreeNode cur2 = null;        TreeNode last = null;//?                TreeNode pre = null;        TreeNode cur = root;        while(cur != null){            //traverse down the left branch            if(cur.left != null){                pre = cur.left;                //find the predecessor                while(pre.right != null && pre.right != cur){                    pre = pre.right;                }                if(pre.right == null){                    pre.right = cur;                    cur = cur.left;                }                else{//pre.right == cur                    pre.right = null;                    last = cur;                    cur = cur.right;                }            }            else{                last = cur;                cur = cur.right;            }            if(last != null && cur != null && last.val > cur.val){                //if the first reversing pair hasn't been set, set it                if(pre1 == null){                    pre1 = last;                    cur1 = cur;                }                else{//Pre1!=null, means that this is the second reversing pair, set it                    pre2 = last;                    cur2 = cur;                }            }        }        int temp = 0;        //If there are two reversing pairs, reverse the 1st in the first pair with the 2nd in the second pair        if(pre1 != null && cur2 != null){            temp = pre1.val;            pre1.val = cur2.val;            cur2.val = temp;        }        //If there is only one reversing pair, reverse the 1st and 2nd in the first pair        else{            temp = pre1.val;            pre1.val = cur1.val;            cur1.val = temp;        }    }}



0 0
原创粉丝点击