LeetCode #99: Recover Binary Search Tree

原创 2016年08月31日 09:33:55

Problem Statement

(Source) 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?

Analysis

Think of the in-order traversal sequence of a binary search tree. It is an increasing sequence. If two elements of the original binary search tree are swapped, then the new in-order traversal sequence would not be in ascending order anymore, but it must fit into one of the following two cases:
(1) There exists only one pair of elements (seq[i], seq[i + 1]) in the new sequence such that seq[i] > seq[i + 1].
(2) There exists two pairs of elements (seq[i], seq[i + 1]), (seq[j], seq[j + 1]) in the new sequence such that seq[i] > seq[i + 1] && seq[j] > seq[j + 1].

Therefore, the idea to solve the problem is to find the first node corresponding to the first element of the first pair, and the last node corresponding to the last element of the last pair, no matter which case the new sequence fits into, and swap them.

Solution below uses an recursive implementation below, which runs in:
- Time complexity: O(n), where n is the total number of the binary search tree.
- Space complexity: O(1).

Solution

# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution(object):
    def recoverTree(self, root):
        """
        :type root: TreeNode
        :rtype: void Do not return anything, modify root in-place instead.
        """
        swapped_nodes = []
        prev = [None]

        def inorder(p, prev, swapped_nodes):
            if p.left:
                inorder(p.left, prev, swapped_nodes)

            if prev[0] and prev[0].val > p.val:
                if len(swapped_nodes) == 0:
                    swapped_nodes.extend([prev[0], p])
                else:
                    swapped_nodes[1] = p
            prev[0] = p

            if p.right:
                inorder(p.right, prev, swapped_nodes)

        inorder(root, prev, swapped_nodes)

        swapped_nodes[0].val, swapped_nodes[1].val = swapped_nodes[1].val, swapped_nodes[0].val
版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

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...

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
  • 247

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
  • 537

【LeetCode】99. Recover Binary Search Tree 分析、解法、注释、中序遍历、递归

99. Recover Binary Search Tree Total Accepted: 50739 Total Submissions: 192395 Difficulty: Hard...

leetcode 99. Recover Binary Search Tree 二叉树非递归遍历,利用stack和morris遍历

一开始觉得就按照二叉搜索树的构造规则由上至下,遇到违反大小规则的节点就直接交换这三个数的值就好了,知道遇到bad case:[3,null,2,null,1],产出结果[2,null,1,null,3...

LeetCode(99)Recover Binary Search Tree

题目如下: Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without c...

LeetCode(99) Recover Binary Search Tree

题目Two elements of a binary search tree (BST) are swapped by mistake.Recover the tree without changin...
  • fly_yr
  • fly_yr
  • 2015年10月15日 14:45
  • 741
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:LeetCode #99: Recover Binary Search Tree
举报原因:
原因补充:

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