hint
BAN USERfresher , looking for a job
copied from another forum .. super cool solution
void postOrderTraversalIterativeTwoStacks(TreeNode *root)
{
if (!root) return;
stack<BinaryTree*> child;
stack<BinaryTree*> parent;
//Initialization
child.push(root);
while (!child.empty()) {
BinaryTree *curr = child.top();
parent.push(curr);
child.pop();
if (curr->left)
child.push(curr->left);
if (curr->right)
child.push(curr->right);
}
//Printing the post order traversal
while (!parent.empty()) {
cout << parent.top()->data << " ";
parent.pop();
}
}
copied from another forum
O(n^2) time and O(1) space algorithm
Rotate by +90:
Transpose
Reverse each row
Rotate by -90:
Transpose
Reverse each column
Rotate by +180:
Method 1: Rotate by +90 twice
Method 2: Reverse each row and then reverse each column
Rotate by -180:
Method 1: Rotate by -90 twice
Method 2: Reverse each column and then reverse each row
Method 3: Reverse by +180 as they are same
agree Eugene , but we can not assume, if this was the code shown the interviewer then my answer will be no memory is allocated , depending on more code answer will change - heap or stack. I am trying to find right answer interviewer is expecting based on information given in the question ... evaluating out the knowledge of memory allocation (ref type or value type , locale/global etc ) in .net requires more code to be shown.
- hint February 18, 2013
This works
- hint May 31, 2015