Advertisement
Guest User

Untitled

a guest
Sep 16th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.77 KB | None | 0 0
  1. typedef struct s_node {
  2.         int           value;
  3.         struct s_node *right;
  4.         struct s_node *left;
  5. } Node;
  6.  
  7. Node* recurse(Node* root) {
  8.  
  9.     if (!root)
  10.         return 0;
  11.  
  12.     if (root->left) {
  13.  
  14.         Node* left = recurse(root->left);
  15.         while (left->right)
  16.             left = left->right;
  17.         left->right = root;
  18.         root->left = left;
  19.  
  20.     }
  21.     if (root->right) {
  22.  
  23.         Node* right = recurse(root->right);
  24.         while (right->left)
  25.             right = right->left;
  26.         right->left = root;
  27.         root->right = right;
  28.     }
  29.     return root;
  30. }
  31.  
  32. Node* convert_bst(Node* bst) {
  33.     if (!bst)
  34.         return 0;
  35.     recurse(bst);
  36.     Node* left = bst;
  37.     Node* right = bst;
  38.     while (left->left)
  39.         left = left->left;
  40.     while (right->right)
  41.         right = right->right;
  42.     left->left = right;
  43.     right->right = left;
  44.     return left;
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement