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
版权声明:本文为博主原创文章,未经博主允许不得转载。

二叉搜索树(Binary Search Tree )的定义及分析

定义:     二叉搜索树或者是一棵空树,或者是具有下列性质的二叉树: 每个结点都有一个作为搜索依据的关键码(key),所有结点的关键码互不相同。 左子树(如果非空)上所...
  • jsh13417
  • jsh13417
  • 2013年01月22日 21:59
  • 3330

二分查找树的C++实现 Binary Search Tree(BST)

用C++实现了二叉查找树的 创建 插入 删除 遍历(宽度优先和三种深度优先[前序,中序,后序]) 查找某值 查找最大值和最小值 清空 复制二叉树(深复制) #include #include u...
  • showonstage1
  • showonstage1
  • 2014年03月13日 19:14
  • 1619

Leetcode-Symmetric Tree——判断二叉树是否对称

题目 Symmetric Tree  Total Accepted: 12814 Total Submissions: 40358My Submissions Given a bina...
  • disappearedgod
  • disappearedgod
  • 2014年04月20日 01:00
  • 2050

leetcode 99 Recover Binary Search Tree (python)

leetcode 99   Recover Binary Search Tree python
  • sxingming
  • sxingming
  • 2016年07月25日 23:37
  • 399

LeetCode 99. Recover Binary Search Tree

1. 题目描述 Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree wit...
  • zhyh1435589631
  • zhyh1435589631
  • 2016年04月08日 12:39
  • 623

leetCode 99: Recover Binary Search Tree

转自:http://blog.csdn.net/worldwindjp/article/details/21694179 Two elements of a binary search tree (...
  • le119126
  • le119126
  • 2015年09月27日 20:53
  • 328

Leetcode 99. Recover Binary Search Tree

99. Recover Binary Search Tree Total Accepted: 52531 Total Submissions: 196781 Difficulty: Ha...
  • fantasiasango
  • fantasiasango
  • 2016年05月23日 11:41
  • 187

(Leetcode 99) Recover Binary Search Tree(恢复二叉排序树BST)

题目原文: Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree with...
  • bcj296050240
  • bcj296050240
  • 2016年05月29日 10:41
  • 498

[leetcode] 99.Recover Binary Search Tree

题目: Two elements of a binary search tree (BST) are swapped by mistake.Recover the tree without chan...
  • u014673347
  • u014673347
  • 2015年08月25日 18:05
  • 191

【一天一道LeetCode】#99. Recover Binary Search Tree

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目...
  • terence1212
  • terence1212
  • 2016年06月24日 21:28
  • 1668
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:LeetCode #99: Recover Binary Search Tree
举报原因:
原因补充:

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