forked from soapyigu/LeetCode-Swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RecoverBinarySearchTree.swift
92 lines (89 loc) · 3.19 KB
/
RecoverBinarySearchTree.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/**
* Question Link: https://leetcode.com/problems/recover-binary-search-tree/
* Primary idea: Morris Traversal (In-order)
* The key is to construct the connection between child & parent
* 1) If cur.left == nil:
* - Output cur.val
* - Set cur = cur.right
* 2) If cur.left != nil:
* - Find the precursor of cur, precursor
* i. If precursor.right == nil:
* - Set precursor.right = cur
* - Set cur = cur.left
* ii. If precursor.right != nil (which means precursor.right === cur):
* - Set precursor.right = nil (Recover the structure of original tree)
* - Output cur.val
* - Set cur = cur.right
* 3) Repeat 1) & 2) until cur == nil
* Time Complexity: O(n), Space Complexity: O(1)
* Recommand Reading: http://www.cnblogs.com/AnnieKim/archive/2013/06/15/morristraversal.html
*
* Definition for a binary tree node.
* public class TreeNode {
* public var val: Int
* public var left: TreeNode?
* public var right: TreeNode?
* public init(_ val: Int) {
* self.val = val
* self.left = nil
* self.right = nil
* }
* }
*/
class RecoverBinarySearchTree {
func recoverTree(_ root: TreeNode?) {
var pre: TreeNode? // Store the pre-node in the sorted list
var first: TreeNode?
var second: TreeNode?
// Morris Traversal
var cur = root
var precursor: TreeNode?
while cur != nil {
// 2) If cur.left != nil:
if cur!.left != nil {
// Find the precursor of cur
precursor = cur!.left
while precursor!.right != nil && precursor!.right !== cur {
precursor = precursor!.right
}
// 2)ii. If the connection already existed
if precursor!.right === cur {
// First time we meet pre.val >= cur.val must be the first node
// But the second node need to be the last time we meet pre.val >= cur.val
// e.g 1, 4, 3, 5, 6 v.s 1, 5, 4, 3, 6
if pre != nil && pre!.val >= cur!.val {
if first == nil {
first = pre
second = cur
} else {
second = cur
}
}
pre = cur!
precursor!.right = nil
cur = cur!.right
// 2)i. Construct the connection
} else {
precursor!.right = cur
cur = cur!.left
}
// 1) If cur.left == nil:
} else {
if pre != nil && pre!.val >= cur!.val {
if first == nil {
first = pre
second = cur
} else {
second = cur
}
}
pre = cur!
cur = cur!.right
}
}
// Swap the 2 nodes
if first != nil && second != nil {
swap(&first!.val, &second!.val)
}
}
}