Hard-题目19:99. Recover Binary Search Tree

原创 2016年05月31日 23:33:33

题目原文:
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?
题目大意:
二叉排序树上的两个节点互换位置了。你能用O(1)的空间复杂度把这两个节点恢复过来吗?
题目分析:
抓住二叉排序树的重要性质——中序遍历递增来解决此题。中序遍历这棵树,如果发现了gap(当前节点比前驱节点值小)则记录下来,要分两种情况,如果gap为1个,就是相邻的两个节点互换了,如果为两个,就是第一个gap的左节点和第二个gap的右节点互换了,分别记录一下互换的节点就好了,每次用一个类成员变量记录上次访问的节点,故空间复杂度为O(1).
源码:(language:java)

public class Solution {
    //private LinkedList<TreeNode> trace = new LinkedList<TreeNode>();
    private int gap = 0;
    private TreeNode lastvisit,node1,node2;
    public void recoverTree(TreeNode root) {
        if(root!=null) {
            inOrderVisit(root);
        }
        int temp = node1.val;
        node1.val = node2.val;
        node2.val = temp;
    }
    private void inOrderVisit(TreeNode root) {
        if(root!=null) {            
            inOrderVisit(root.left);
            if(lastvisit!=null && root.val<lastvisit.val) {
                if(gap == 0) 
                    node1 = lastvisit;  
                node2 = root;
                gap++;
            }
            lastvisit = root;
            inOrderVisit(root.right);
        }       
    }
}

成绩:
4ms,beats 42.88%,众数4ms,54.80%
Cmershen的碎碎念:
本学妹不是很清楚,二叉树递归算O(logn)空间复杂度吗?

版权声明:完整版Leetcode题解请出门左转https://github.com/cmershen1/leetcode/tree/master/docs

leetcode 99. Recover Binary Search Tree BST二叉搜索树的两元素交换的恢复 + 中序遍历

Two elements of a binary search tree (BST) are swapped by mistake.Recover the tree without changing ...

leetcode_99_Recover Binary Search Tree

思路: 首先中序遍历二叉查找树并将遍历的节点存储到一个list中,然后对list中的值进行比较,查找出位置出现变化的两个结点,将两个结点的值进行互换,完成本题的要求。 但是呢,对于如何发现位置出现变...

leetcode 99. Recover Binary Search Tree

Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without chang...

LeetCode 99 Recover Binary Search Tree

Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without chang...

99:Recover Binary Search Tree【树】【DFS】

题目链接:click~ /*题意:二叉搜索树种有两个结点被调换,要求将它们找出来并恢复*/ /** *思路:中序遍历二叉树时,记录第一次违反有序规则的两个结点,这两个结点中的 * 第...

LeetCode 99. Recover Binary Search Tree(修复二叉搜索树)

原题网址:https://leetcode.com/problems/recover-binary-search-tree/ Two elements of a binary search tre...
  • jmspan
  • jmspan
  • 2016年05月23日 00:39
  • 263

LeetCode(99) Recover Binary Search Tree解题报告

Two elements of a binary search tree (BST) are swapped by mistake.Recover the tree without changing ...

LeetCode --- 99. Recover Binary Search Tree

题目链接:Recover Binary Search Tree Two elements of a binary search tree (BST) are swapped by mistake. ...
  • makuiyu
  • makuiyu
  • 2015年04月14日 19:52
  • 559

leetcode 99 Recover Binary Search Tree (python)

leetcode 99   Recover Binary Search Tree python

LeetCode 99. Recover Binary Search Tree

1. 题目描述 Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree wit...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Hard-题目19:99. Recover Binary Search Tree
举报原因:
原因补充:

(最多只允许输入30个字)