Node binary tree, there range itself means there are two pointers of left children and right children; a doubly linked list of nodes, there range itself, has a pointer pointing to the previous node and the next node . Structurally, the two structures have similarities, there are a binary tree search, convert it to an ordered doubly linked list.
1 #include <iostream> 2 #include <queue> 3 using namespace std; 4 struct treeNode 5 { 6 int v; 7 treeNode *l, *r; 8 treeNode(int a = -1) :v(a), l(nullptr), r(nullptr) {} 9 }; 10 struct listNode 11 { 12 int v; 13 listNode *pre, *next; 14 listNode(int a = -1) :v(a), pre(nullptr), next(nullptr) {} 15 }; 16 treeNode* root = new treeNode(6); 17 listNode* head = new listNode(-1); 18 queue<int>qD; 19 void creatTree() 20 { 21 root->l = new treeNode(4); 22 root->r = new treeNode(7); 23 root->l->l = new treeNode(2); 24 root->l->r = new treeNode(5); 25 root->l->l->l = new treeNode(1); 26 root->l->l->r = new treeNode(3); 27 root->r->r = new treeNode(9); 28 root->r->l = new treeNode(8); 29 } 30 void getData(treeNode* rt) 31 { 32 if (rt == nullptr) 33 return; 34 getData(rt->l); 35 qD.push(rt->v); 36 getData(rt->r); 37 } 38 void creatList() 39 { 40 listNode* p = head; 41 while (!qD.empty()) 42 { 43 listNode* q = new listNode(qD.front()); 44 qD.pop(); 45 p->next = q; 46 q->pre = p; 47 p = q; 48 } 49 } 50 void printD() 51 { 52 listNode* p = head->next; 53 while (p != nullptr) 54 { 55 cout << p->v << " "; 56 p = p->next; 57 } 58 } 59 int main() 60 { 61 creatTree(); 62 getData(root); 63 creatList(); 64 printD(); 65 return 0; 66 }