https://leetcode.com/problems/recover-binary-search-tree/description/
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] 3 / \ 1 4 / 2 Output: [2,1,4,null,null,3] 2 / \ 1 4 / 3
Follow up:
- A solution using O(n) space is pretty straight forward.
- Could you devise a constant space 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.
"""
self.pre_node,self.n1,self.n2 = None,None,None
self.solve(root)
self.n1.val,self.n2.val=self.n2.val,self.n1.val
def solve(self,root):
if not root:return
self.solve(root.left)
if not self.pre_node:self.pre_node = root
if root.val<self.pre_node.val:
if not self.n1:
self.n1 = self.pre_node
self.n2 = root
else:self.n2 = root
self.pre_node = root
self.solve(root.right)