You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We can observe that the resultant tree is same as preorder traversal of binary tree.
so we get preorder traversal of tree and start creating resultant tree. It's right will be next node in traversal and left will be null.
TC: O(N)
SC: O(N)
Code
classSolution {
private:voidpreorder(TreeNode* root, vector<int>& ans)
{
if (root == NULL)
return;
ans.push_back(root->val);
preorder(root->left, ans);
preorder(root->right, ans);
}
public:voidflatten(TreeNode* root)
{
vector<int> ans;
preorder(root, ans);
int n = ans.size();
for (int i = 1; i < n; i++) {
root->right = newTreeNode(ans[i]);
root->left = NULL;
root = root->right;
}
return;
}
};
Postorder Traversal with right node first
We can observe that the resultant tree is same as postorder traversal of binary tree with right node traversed first and root's right is previous node of traversal.
TC:O(N)
SC:O(N)
Code
classSolution {
private:
TreeNode* prev = NULL;
public:voidflatten(TreeNode* root)
{
// classic postorder traversal, with prev value assigned to root->right and root->left as nullif (root == NULL)
return;
flatten(root->right);
flatten(root->left);
root->right = prev;
root->left = NULL;
prev = root;
}
};
Iterative version of above solution
Code
classSolution {
public:voidflatten(TreeNode* root)
{
stack<TreeNode*> st;
st.push(root);
while (!st.empty()) {
TreeNode* cur = st.top();
st.pop();
if (cur) {
if (cur->right) st.push(cur->right);
if (cur->left) st.push(cur->left);
cur->left = NULL;
if (!st.empty()) cur->right = st.top();
}
}
}
};
Morris Traversal
To optimize the O(N) space complexity we will use morris traversal
This algorithm modifies the given binary tree in-place to transform it into a linked list where the right child of each 1. node becomes the next node in the list
Start at the root node.
Initialize the current node as the root.
While the current node is not null:
a. If the current node has no left child, visit the current node and move to its right child.
b. If the current node has a left child:
i. Find the rightmost node in the left subtree of the current node. This node is called the "predecessor."
ii. If the predecessor's right pointer is null, set it to point to the current node, move to the left child, and go back to step 3.
iii. If the predecessor's right pointer is already pointing to the current node, restore the original tree structure, visit the current node, and move to its right child.
Repeat steps 3 until the current node becomes null.
Code
classSolution {
public:voidflatten(TreeNode* root){
// morris traversal
TreeNode* cur = root;
while (cur){
if (cur->left){
TreeNode* pre = cur->left;
while (pre->right)
pre = pre->right;
pre->right = cur->right;
cur->right = cur->left;
cur->left = NULL;
}
cur = cur->right;
}
}
};