1 // question: 2 // enter a binary search tree and convert it into a sorted two-way linked list. 3 // you cannot create any new node. You only need to adjust the pointer. 4 5 // 10 6 // 7 // 6 14 8 // 9 // 4 8 12 1610 11 // convert to a two-way linked list 12 // 4 = 6 = 8 = 10 = 12 = 14 = 16. 13 14 # include <iostream> 15 using namespace STD; 16 17 class node {18 public: 19 int data; 20 node * left; 21 node * right; 22 23 node (int d = 0, node * LR = 0, node * RR = 0): Data (D), left (LR), right (RR) {} 24}; 25 26 node * Create () 27 {28 node * root; 29 node * P4 = new node (4 ); 30 node * P8 = new node (8); 31 node * P6 = new node (6, P4, P8); 32 33 node * p12 = new node (12 ); 34 node * p16 = new node (16); 35 node * p14 = ne W node (14, p12, p16); 36 37 node * P10 = new node (10, P6, p14); 38 root = P10; 39 40 return root; 41} 42 43 node * Change (node * P, bool asright) 44 {45 if (! P) 46 return NULL; 47 node * pleft = change (P-> left, false); 48 if (pleft) 49 pleft-> right = P; 50 p-> left = pleft; 51 52 node * pright = change (P-> right, true); 53 If (pright) 54 pright-> left = P; 55 p-> right = pright; 56 57 node * r = P; 58 If (asright) 59 {60 while (R-> left) 61 r = r-> left; 62} else {63 while (R-> right) 64 r = r-> right; 65} 66 return r; 67} 68 69 int main () {70 node * root = create (); 71 node * tail = change (root, false); 72 while (tail) 73 {74 cout <tail-> data <""; 75 tail = tail-> left; 76} 77 cout <Endl; 78 79 root = create (); 80 node * head = change (root, true); 81 While (head) 82 {83 cout
IT company 100 question-1