Vous êtes sur la page 1sur 6

Check if two binary trees are identical or not | Iterative & Recursive - Techie Delight

// Iterative function to check if given binary trees are identical or not


bool isIdentical(Node *x, Node *y)
{
// if both trees are empty, return true
if (x == nullptr && y == nullptr)
return true;

// if first tree is empty (& second tree is non-empty), return false


if (x == nullptr)
return false;

// if second tree is empty (& first tree is non-empty), return false


if (y == nullptr)
return false;

// create a stack to hold Node* pairs


stack<pair<Node*, Node*>> stack;
stack.push({x, y});

// do till stack is not empty


while (!stack.empty())
{
// pop top pair from the stack and process it
Node *x = stack.top().first;
Node *y = stack.top().second;
stack.pop();

// if value of their root node don't match, return false


if (x->key != y->key)
return false;

// if left subtree of both x and y exists, push their addresses


// to stack else return false if only one left child exists

if (x->left && y->left)


stack.push({x->left, y->left});

else if (x->left || y->left)


return false;

// if right subtree of both x and y exists, push their addresses


// to stack else return false if only one right child exists

if (x->right && y->right)


stack.push({x->right, y->right});

else if (x->right || y->right)


return false;
}

// if we reach here, both binary trees are identical


return true;
}
Find ancestors of given node in a Binary Tree - Techie Delight
void printTopToBottomPath(unordered_map<int, int> parent, int key)
{
while (key = parent[key])
cout << key << " ";

cout << endl;


}

// 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);

// run till stack is not empty


while (!stack.empty())
{
// Pop the top item from stack
Node* curr = stack.top();
stack.pop();

// 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);
}
}
}

// Iterative function to print all ancestors of given node in binary tree


void printAncestors(Node* root, int node)
{
// Base Case
if (root == nullptr)
return;

// create a empty map to store parent pointers of binary tree nodes


unordered_map<int, int> parent;
// set parent of root node as null
parent[root->data] = 0;

// set parent nodes for all nodes of binary tree


setParent(root, parent);

// print ancestors of given node using parent map


printTopToBottomPath(parent, node);
}
Invert given Binary Tree | Recursive and Iterative solution - Techie Delight
void invertBinaryTree(Node* root)
{
// base case: if tree is empty
if (root == nullptr)
return;

// create an empty stack and push root node


stack<Node*> s;
s.push(root);

// run till stack is not empty


while (!s.empty())
{
// pop top node from stack
Node* curr = s.top();
s.pop();

// swap left child with right child


swap(curr->left, curr->right);

// push right child of popped node to the stack


if (curr->right)
s.push(curr->right);

// push left child of popped node to the stack


if (curr->left)
s.push(curr->left);
}
}
Level order traversal in spiral form - GeeksforGeeks
http://www.geeksforgeeks.org/level-order-traversal-in-spiral-form/

void printSpiral(struct node *root)


{
if (root == NULL) return; // NULL check

// Create two stacks to store alternate levels


stack<struct node*> s1; // For levels to be printed from right to left
stack<struct node*> s2; // For levels to be printed from left to right

// Push first level to first stack 's1'


s1.push(root);

// Keep ptinting while any of the stacks has some nodes


while (!s1.empty() || !s2.empty())
{
// Print nodes of current level from s1 and push nodes of
// next level to s2
while (!s1.empty())
{
struct node *temp = s1.top();
s1.pop();
cout << temp->data << " ";

// Note that is right is pushed before left


if (temp->right)
s2.push(temp->right);
if (temp->left)
s2.push(temp->left);
}

// Print nodes of current level from s2 and push nodes of


// next level to s1
while (!s2.empty())
{
struct node *temp = s2.top();
s2.pop();
cout << temp->data << " ";

// Note that is left is pushed before right


if (temp->left)
s1.push(temp->left);
if (temp->right)
s1.push(temp->right);
}
}
}

Iterative Postorder Traversal | Set 1 (Using Two Stacks) - GeeksforGeeks


http://www.geeksforgeeks.org/iterative-postorder-traversal/

void postOrderIterative(struct Node* root)


{
if (root == NULL)
return;

// Create two stacks


struct Stack* s1 = createStack(MAX_SIZE);
struct Stack* s2 = createStack(MAX_SIZE);

// push root to first stack


push(s1, root);
struct Node* node;

// Run while first stack is not empty


while (!isEmpty(s1))
{
// Pop an item from s1 and push it to s2
node = pop(s1);
push(s2, node);

// Push left and right children of removed item to s1


if (node->left)
push(s1, node->left);
if (node->right)
push(s1, node->right);
}

// Print all elements of second stack


while (!isEmpty(s2))
{
node = pop(s2);
printf("%d ", node->data);
}
}

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/

bool canRepresentBST(int pre[], int n)


{
// Create an empty stack
stack<int> s;

// Initialize current root as minimum possible


// value
int root = INT_MIN;

// Traverse given array


for (int i=0; i<n; i++)
{
// If we find a node who is on right side
// and smaller than root, return false
if (pre[i] < root)
return false;

// If pre[i] is in right subtree of stack top,


// Keep removing items smaller than pre[i]
// and make the last removed item as new
// root.
while (!s.empty() && s.top()<pre[i])
{
root = s.top();
s.pop();
}

// At this point either stack is empty or


// pre[i] is smaller than root, push pre[i]
s.push(pre[i]);
}
return true;
}

Vous aimerez peut-être aussi