|
| 1 | +#include <bits/stdc++.h> |
| 2 | + |
| 3 | +using namespace std; |
| 4 | + |
| 5 | +class Node{ |
| 6 | + public: |
| 7 | + Node* left; |
| 8 | + Node* right; |
| 9 | + int data; |
| 10 | + Node(int x){ |
| 11 | + data = x; |
| 12 | + left = NULL; |
| 13 | + right = NULL; |
| 14 | + } |
| 15 | +}; |
| 16 | + |
| 17 | +class Solution{ |
| 18 | + public: |
| 19 | + Node* insert(Node* root, int data) { |
| 20 | + if(root == NULL) { |
| 21 | + return new Node(data); |
| 22 | + } else { |
| 23 | + Node* cur; |
| 24 | + if(data <= root->data) { |
| 25 | + cur = insert(root->left, data); |
| 26 | + root->left = cur; |
| 27 | + } else { |
| 28 | + cur = insert(root->right, data); |
| 29 | + root->right = cur; |
| 30 | + } |
| 31 | + |
| 32 | + return root; |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + Node* flatten(Node* A) { |
| 37 | + if(!A)return NULL; |
| 38 | + |
| 39 | + // Take the left and right subtree |
| 40 | + Node* tempr = A->right; |
| 41 | + Node* templ = A->left; |
| 42 | + |
| 43 | + // Recursively Flatten them |
| 44 | + flatten(templ); |
| 45 | + flatten(tempr); |
| 46 | + |
| 47 | + // If left subtree exists, place it as the right subtree and make the right subtree |
| 48 | + // as the child of this new right subtree |
| 49 | + if(templ){ |
| 50 | + A->right = templ; |
| 51 | + Node* x = templ; |
| 52 | + // Go to the end of the right subtree (which was earlier left subtree) |
| 53 | + while(x->right){ |
| 54 | + x = x->right; |
| 55 | + } |
| 56 | + // Place the earlier right subtree as the right subtree of the current right subtree |
| 57 | + x->right = tempr; |
| 58 | + |
| 59 | + // Make the new left subtree as NULL |
| 60 | + templ->left = NULL; |
| 61 | + } |
| 62 | + |
| 63 | + // Nullify the left subtree. |
| 64 | + A->left = NULL; |
| 65 | + |
| 66 | + // Return the tree. |
| 67 | + return A; |
| 68 | + } |
| 69 | + |
| 70 | + void inOrder(Node* tree){ |
| 71 | + if(!tree) return; |
| 72 | + |
| 73 | + inOrder(tree->left); |
| 74 | + cout << tree->data << " " ; |
| 75 | + inOrder(tree->right); |
| 76 | + } |
| 77 | +}; |
| 78 | + |
| 79 | +// Initial Tree: |
| 80 | +// 4 |
| 81 | +// / \ |
| 82 | +// 2 7 |
| 83 | +// / \ / |
| 84 | +// 1 3 6 |
| 85 | + |
| 86 | +// Final Tree: |
| 87 | +// 4 |
| 88 | +// / \ |
| 89 | +// N 2 |
| 90 | +// / \ |
| 91 | +// N 1 |
| 92 | +// / \ |
| 93 | +// N 3 |
| 94 | +// / \ |
| 95 | +// N 7 |
| 96 | +// / \ |
| 97 | +// N 6 |
| 98 | +// N - NULL |
| 99 | + |
| 100 | + |
| 101 | +int main() { |
| 102 | + Solution myTree; |
| 103 | + Node* root = NULL; |
| 104 | + root = myTree.insert(root,4); |
| 105 | + root = myTree.insert(root,2); |
| 106 | + root = myTree.insert(root,3); |
| 107 | + root = myTree.insert(root,1); |
| 108 | + root = myTree.insert(root,7); |
| 109 | + root = myTree.insert(root,6); |
| 110 | + |
| 111 | + cout << "InOrder Traversal is of the tree is as follows: " << endl; |
| 112 | + myTree.inOrder(root); |
| 113 | + cout << endl; |
| 114 | + |
| 115 | + |
| 116 | + myTree.flatten(root); |
| 117 | + cout << "\n\nInOrder Traversal is of the flattened tree is as follows: " << endl; |
| 118 | + myTree.inOrder(root); |
| 119 | + cout << endl; |
| 120 | + return 0; |
| 121 | +} |
| 122 | + |
0 commit comments