diff --git a/31. In Order Morris Traversal In Binarytree.cpp b/31. In Order Morris Traversal In Binarytree.cpp new file mode 100644 index 0000000..bd4551d --- /dev/null +++ b/31. In Order Morris Traversal In Binarytree.cpp @@ -0,0 +1,99 @@ +#include +#include +#include +using namespace std; + +class TreeNode +{ +public: + int val = 0; + TreeNode* left = nullptr; + TreeNode* right = nullptr; + + TreeNode(int val) + { + this->val = val; + } +}; + +vector morrisInTraversal(TreeNode* root) { + + vector ans; + if(!root) return ans; + + TreeNode* curr = root; + + while(curr != NULL) { + + if(curr->left == NULL) { + ans.push_back(curr->val); + curr = curr->right; + } + else { + TreeNode* prev = curr->left; + while(prev->right != NULL && prev->right != curr) { + prev = prev->right; + } + + if(prev->right == NULL) { + prev->right = curr; + curr = curr->left; + } + + if(prev->right == curr) { + ans.push_back(curr->val); + prev->right = NULL; + curr = curr->right; + } + + } + } + + return ans; + +} + +// input_section================================================= + +TreeNode* createTree(vector& arr, vector& IDX) +{ + + if (IDX[0] > arr.size() || arr[IDX[0]] == -1) + { + IDX[0]++; + return nullptr; + } + + TreeNode* node = new TreeNode(arr[IDX[0]++]); + node->left = createTree(arr, IDX); + node->right = createTree(arr, IDX); + + return node; +} + +void solve() +{ + int n; + cin >> n; + vector arr(n, 0); + for (int i = 0; i < n; i++) + { + cin >> arr[i]; + } + + vector IDX(1, 0); + TreeNode* root = createTree(arr, IDX); + + vector ans = morrisInTraversal(root); + + for (int i : ans) + { + cout << i << " "; + } +} + +int main() +{ + solve(); + return 0; +} \ No newline at end of file diff --git a/33. Flatten Binary Tree to Linked List.cpp b/33. Flatten Binary Tree to Linked List.cpp new file mode 100644 index 0000000..d8532f9 --- /dev/null +++ b/33. Flatten Binary Tree to Linked List.cpp @@ -0,0 +1,86 @@ +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode() : val(0), left(nullptr), right(nullptr) {} + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} + * }; + */ +class Solution { + TreeNode* prev = NULL; + +public: + void flatten(TreeNode* root) { + + /* + // Recursive Aproach: + + if(root == NULL) return; + + flatten(root->right); + flatten(root->left); + + root->right = prev; + root->left = NULL; + + prev = root; + */ + + /* + // Iterative Approach + + if(root == NULL) return; + + stack st; + st.push(root); + + while(!st.empty()) { + + TreeNode* curr = st.top(); + st.pop(); + + if(curr->right) st.push(curr->right); + if(curr->left) st.push(curr->left); + + if(st.empty() == false) { + curr->right = st.top(); + } + + curr->left = NULL; + + } + */ + + + // Morris Traversal Approach + + if(root == NULL) return; + + TreeNode* curr = root; + + while(curr != NULL) { + + if(curr->left) { + TreeNode* prev = curr->left; + + while(prev->right) { + prev = prev->right; + } + + prev->right = curr->right; + curr->right = curr->left; + curr->left = NULL; + + } + + curr = curr->right; + + } + + + + } +}; \ No newline at end of file