category-wise-problems

contains category wise problems(data structures, competitive) of popular platforms.

View the Project on GitHub mayankdutta/category-wise-problems

Binary tree to DLL

DFS

Implementation ```cpp class Solution { public: Node *head; Node *prev; void dfs(Node* root) { if (root == nullptr) { return; } dfs(root->left); prev->right = root; root->left = prev; prev = root; dfs(root->right); } Node * bToDLL(Node *root) { head = new Node(); prev = head; dfs(root); head = head->right; head->left = nullptr; return head; } }; ```

Morris

NOTE:

PREV in the Morris traversal always points to the PARENT OF ROOT iff the linking is done, to the bottom-est child otherwise.
Implementation ```cpp class Solution { public: Node * bToDLL(Node *root) { Node *head = new Node(); Node *dummy = head; while (root) { if (root->left != nullptr) { auto prev = root->left; while (prev->right != nullptr && prev != root) { prev = prev->right; } if (prev == root) { root->left = dummy; dummy->right = root; dummy = root; root = root->right; } else { prev->right = root; root = root->left; } } else { root->left = dummy; dummy->right = root; dummy = root; root = root->right; } } head = head->right; head->left = nullptr; return head; } }; ```