-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbsttoDLL
75 lines (58 loc) · 1.57 KB
/
bsttoDLL
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
已知一个BST(binary search tree), 将其原地转化成一个循环的排序过的双链表(circular sorted double linked list)。
说明:BST的节点有两个指针left, right, 分别指向比它小,和比它大的节点。变成DLL之后,
由于DLL节点原本有prev 和 next 指针分别和之前和之后的节点,这里假定原left指针指向之前,原right 指向之后节点。关于题意可以参考下图:
struct TreeNode {
int val;
TreeNode* left;
TreeNode* right;
};
TreeNode* bsttoDLL (TreeNode* root) {
TreeNode* head = NULL;
TreeNode* preNode = NULL;
inordertrav(root, preNode, head);
return newHead;
}
void inordertrav(TreeNode* node, TreeNode* preNode, TreeNode* head) {
if (!node) {
return NULL;
}
inordertrav(node->left, preNode, head);
node->left = preNode;
if (!preNode) {
head = node;
} else {
preNode->right = node;
}
TreeNode* right = node->right;
head->left = node;
node->right = head;
inorderTrav(right, node, head);
return;
}
TreeNode* bfs2list(TreeNode* node) {
if (!node) {
return NULL;
}
TreeNode* leftList = bfs2list(node->left);
TreeNode* rightList = bfs2list(node->right);
node->left = node;
node->right = node;
leftList = append(leftList, node);
leftList = append(leftList, rightList);
return leftList;
}
TreeNode* append(TreeNode* left, TreeNode* right) {
if (!left) {
return right;
}
if (!right) {
return left;
}
TreeNode *alast = left->left;
TreeNode *blast = right->left;
alast->right = right;
right->left = alast;
left->left = blast;
blast->right = left;
return left;
}