Académique Documents
Professionnel Documents
Culture Documents
// iterative function to set parent nodes for all nodes of binary tree
// in given map. The function is similar to iterative pre-order traversal
void setParent(Node* root, unordered_map<int, int> &parent)
{
// create an empty stack and push root node to it
stack<Node*> stack;
stack.push(root);
// push its right child to stack and set its parent in the map.
// Since right child is pushed first, left child will be
// processed before
if (curr->right)
{
parent[curr->right->data] = curr->data;
stack.push(curr->right);
}
// push its left child to stack and set its parent in the map
if (curr->left)
{
parent[curr->left->data] = curr->data;
stack.push(curr->left);
}
}
}
Check if a given array can represent Preorder Traversal of Binary Search Tree -
GeeksforGeeks
http://www.geeksforgeeks.org/check-if-a-given-array-can-represent-preorder-traversal-of-binary-
search-tree/