-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbintree_to_doublelist.c
executable file
·38 lines (33 loc) · 1.02 KB
/
bintree_to_doublelist.c
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
#include<iostream>
struct node{
int value;
struct node* pleft;
struct node* pright;
};
//plastnode to rember the lastnode in double list
//use the inorder print
void change(struct node* root, struct node** plastnode){
if(root == NULL)
return;
struct node* cur = root;
if(cur->pleft != NULL)
change(cur->pleft,plastnode);
cur->pleft = *plastnode;
if(*plastnode != NULL)
//have the node in the double list
(*plastnode)->pright = cur;
*plastnode = cur;
if(cur->pright != NULL)
change(cur->pright,plastnode);
}
struct node* to_double_list(struct node* root){
if(root == NULL)
return NULL;
struct node* plastnode = NULL;
change(root, &plastnode);
//return the head of the double list;
while(plastnode->pleft != NULL && plastnode != NULL){
plastnode = plastnode->pleft;
}
return plastnode;
}