Given a binary tree, flatten it to a linked list in-place.
For example,
Given
1 / \ 2 5 / \ \ 3 4 6
The flattened tree should look like:
1 \ 2 \ 3 \ 4 \ 5 \ 6
Hints:
If you notice carefully in the flattened tree, each node's right child points to the next node of a pre-order traversal.
class Solution {
public:
void flatten(TreeNode *root) {
if (!root) return;
TreeNode *temp;
stack<TreeNode*> st;
st.push(root);
while (!st.empty()) {
temp = st.top(), st.pop();
if (!temp->left && !temp->right && !st.empty()) {
temp->right = st.top();
continue;
}
if (temp->right) st.push(temp->right);
if (temp->left) {
st.push(temp->left);
temp->right = temp->left;
temp->left = NULL;
}
}
}
};
Notice that if (temp->right) is before if (temp->left) to make sure that it is a preorder visiting.