Advertisement
Josif_tepe

Untitled

Mar 20th, 2024
624
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.57 KB | None | 0 0
  1. class Solution
  2. {
  3. public:
  4.     void inorder_dfs(Node * node, Node * & parent) {
  5.         if(node == NULL) {
  6.             return;
  7.         }
  8.         inorder_dfs(node->left, parent);
  9.         parent->left = NULL;
  10.         parent->right = node;
  11.         parent = node;
  12.         inorder_dfs(node->right, parent);
  13.     }
  14.     Node *flattenBST(Node *root)
  15.     {
  16.         Node * tmp = new Node(-1);
  17.         Node * parent = tmp;
  18.         inorder_dfs(root, parent);
  19.         parent->left = NULL;
  20.         parent->right = NULL;
  21.         Node * res = tmp->right;
  22.         return res;
  23.     }
  24. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement