-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLCR155-ConvertBinarySearchTreeToSortedDoublyLinkedList.go
More file actions
184 lines (161 loc) · 4.94 KB
/
LCR155-ConvertBinarySearchTreeToSortedDoublyLinkedList.go
File metadata and controls
184 lines (161 loc) · 4.94 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package main
// LCR 155. 将二叉搜索树转化为排序的双向链表
// 将一个 二叉搜索树 就地转化为一个 已排序的双向循环链表 。
// 对于双向循环列表,你可以将左右孩子指针作为双向循环链表的前驱和后继指针,第一个节点的前驱是最后一个节点,最后一个节点的后继是第一个节点。
// 特别地,我们希望可以 就地 完成转换操作。
// 当转化完成以后,树中节点的左指针需要指向前驱,树中节点的右指针需要指向后继。
// 还需要返回链表中最小元素的指针。
// 示例 1:
// 输入:root = [4,2,5,1,3]
// <img src="https://assets.leetcode.com/uploads/2018/10/12/bstdllreturndll.png" />
// 输出:[1,2,3,4,5]
// 解释:下图显示了转化后的二叉搜索树,实线表示后继关系,虚线表示前驱关系。
// <img src="https://assets.leetcode.com/uploads/2018/10/12/bstdllreturnbst.png" />
// 示例 2:
// 输入:root = [2,1,3]
// 输出:[1,2,3]
// 示例 3:
// 输入:root = []
// 输出:[]
// 解释:输入是空树,所以输出也是空链表。
// 示例 4:
// 输入:root = [1]
// 输出:[1]
// 提示:
// -1000 <= Node.val <= 1000
// Node.left.val < Node.val < Node.right.val
// Node.val 的所有值都是独一无二的
// 0 <= Number of Nodes <= 2000
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
}
// /*
// // Definition for a Node.
// class Node {
// public:
// int val;
// Node* left;
// Node* right;
// Node() {}
// Node(int _val) {
// val = _val;
// left = NULL;
// right = NULL;
// }
// Node(int _val, Node* _left, Node* _right) {
// val = _val;
// left = _left;
// right = _right;
// }
// };
// */
// class Solution {
// public:
// Node* treeToDoublyList(Node* root) {
// if(root == nullptr) {
// return (Node*)nullptr;
// }
// // 中序遍历
// stack<Node*> st;
// Node* first = nullptr;
// Node* pre = nullptr;
// while(root != nullptr || !st.empty()) {
// // 左子树全部入栈
// while(root != nullptr) {
// st.push(root);
// root = root->left;
// }
// root = st.top(), st.pop();
// if(first == nullptr) {
// first = root;
// }
// if(pre != nullptr) {
// pre->right = root;
// root->left = pre;
// }
// pre = root;
// root = root->right;
// }
// first->left = pre;
// pre->right = first;
// return first;
// }
// };
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)
}