-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path426-ConvertBinarySearchTreeToSortedDoublyLinkedList.go
More file actions
165 lines (152 loc) · 4.98 KB
/
426-ConvertBinarySearchTreeToSortedDoublyLinkedList.go
File metadata and controls
165 lines (152 loc) · 4.98 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package main
// 426. Convert Binary Search Tree to Sorted Doubly Linked List
// Convert a Binary Search Tree to a sorted Circular Doubly-Linked List in place.
// You can think of the left and right pointers as synonymous to the predecessor and successor pointers in a doubly-linked list.
// For a circular doubly linked list, the predecessor of the first element is the last element,
// and the successor of the last element is the first element.
// We want to do the transformation in place.
// After the transformation, the left pointer of the tree node should point to its predecessor,
// and the right pointer should point to its successor.
// You should return the pointer to the smallest element of the linked list.
// Example 1:
// <img src="https://assets.leetcode.com/uploads/2018/10/12/bstdlloriginalbst.png" />
// Input: root = [4,2,5,1,3]
// <img src="https://assets.leetcode.com/uploads/2018/10/12/bstdllreturndll.png" />
// Output: [1,2,3,4,5]
// <img src="https://assets.leetcode.com/uploads/2018/10/12/bstdllreturnbst.png" />
// Explanation: The figure below shows the transformed BST. The solid line indicates the successor relationship, while the dashed line means the predecessor relationship.
// Example 2:
// Input: root = [2,1,3]
// Output: [1,2,3]
// Constraints:
// The number of nodes in the tree is in the range [0, 2000].
// -1000 <= Node.val <= 1000
// All the values of the tree are unique.
import "fmt"
// Definition for a Node.
type Node struct {
Val int
Left *Node
Right *Node
}
/**
* Definition for a Node.
* type Node struct {
* Val int
* Left *Node
* Right *Node
* }
*/
func treeToDoublyList(root *Node) *Node {
//中序遍历
// 把前后连接,每次返回前一个节点,并连接
if root == nil {
return nil
}
var inorder func(root *Node) (left,right *Node)
inorder = func(root *Node) (left,right *Node) {
if root == nil {
return nil, nil
}
l1,r1 := inorder(root.Left)
l2,r2 := inorder(root.Right)
if root.Left == nil && root.Right == nil {
return root, root
} else if root.Left == nil && root.Right != nil {
root.Right = l2
l2.Left = root
return root, r2
} else if root.Left != nil && root.Right == nil {
root.Left = r1
r1.Right = root
return l1, root
} else {
root.Left = r1
r1.Right = root
root.Right = l2
l2.Left = root
return l1, r2
}
}
left, right := inorder(root)
left.Left = right
right.Right = left
return left
}
func treeToDoublyList1(root *Node) *Node {
var dfs func(root *Node) (*Node, *Node)
dfs = func(root *Node) (*Node, *Node) {
if root == nil { return nil, nil }
leftMax, leftMin := dfs(root.Left)
rightMax, rightMin := dfs(root.Right)
if leftMax != nil {
leftMax.Right = root
root.Left = leftMax
}
if rightMin != nil {
rightMin.Left = root
root.Right = rightMin
}
if leftMin == nil {
leftMin = root
}
if rightMax == nil {
rightMax = root
}
return rightMax, leftMin
}
tail, head := dfs(root)
if head != nil && tail != nil {
head.Left = tail
tail.Right = head
}
return head
}
func main() {
// Example 1:
// <img src="https://assets.leetcode.com/uploads/2018/10/12/bstdlloriginalbst.png" />
// Input: root = [4,2,5,1,3]
// <img src="https://assets.leetcode.com/uploads/2018/10/12/bstdllreturndll.png" />
// Output: [1,2,3,4,5]
// <img src="https://assets.leetcode.com/uploads/2018/10/12/bstdllreturnbst.png" />
// Explanation: The figure below shows the transformed BST. The solid line indicates the successor relationship, while the dashed line means the predecessor relationship.
tree1 := &Node {
4,
&Node{2, &Node{1, nil, nil}, &Node{3, nil, nil}, },
&Node{5, nil, nil},
}
t1 := treeToDoublyList(tree1)
fmt.Println("t1 ", t1)
fmt.Println("t1.Left ", t1.Left)
fmt.Println("t1.Right ", t1.Right)
// Example 2:
// Input: root = [2,1,3]
// Output: [1,2,3]
tree2 := &Node {
2,
&Node{1, nil, nil},
&Node{3, nil, nil},
}
t2 := treeToDoublyList(tree2)
fmt.Println("t2 ", t2)
fmt.Println("t2.Left ", t2.Left)
fmt.Println("t2.Right ", t2.Right)
tree11 := &Node {
4,
&Node{2, &Node{1, nil, nil}, &Node{3, nil, nil}, },
&Node{5, nil, nil},
}
t11 := treeToDoublyList1(tree11)
fmt.Println("t11 ", t11)
fmt.Println("t11.Left ", t11.Left)
fmt.Println("t11.Right ", t11.Right)
tree12 := &Node {
2,
&Node{1, nil, nil},
&Node{3, nil, nil},
}
t12 := treeToDoublyList1(tree12)
fmt.Println("t12 ", t12)
fmt.Println("t12.Left ", t12.Left)
fmt.Println("t12.Right ", t12.Right)
}