Professional Documents
Culture Documents
if (front->left)
queue.push_back(front->left);
if (front->right)
queue.push_back(front->right);
}
return height;
}
Delete given Binary Tree | Iterative & Recursive - Techie Delight
void deleteBinaryTree(Node* &root)
{
// empty tree
if (root == nullptr)
return;
if (front->left)
queue.push(front->left);
if (front->right)
queue.push(front->right);
return true;
}
Invert given Binary Tree | Recursive and Iterative solution - Techie Delight
void invertBinaryTree(Node* root)
{
// base case: if tree is empty
if (root == nullptr)
return;
if (node->right != nullptr)
q.push(node->right);
}
while (1)
{
// nodeCount (queue size) indicates number of nodes
// at current lelvel.
int nodeCount = q.size();
if (nodeCount == 0)
break;
// Initialize result
int result = 0;
return result;
}
Check whether a given Binary Tree is Complete or not | Set 1 (Iterative Solution) -
GeeksforGeeks
http://www.geeksforgeeks.org/check-if-a-given-binary-tree-is-complete-tree-or-not/
bool isCompleteBT(struct node* root)
{
// Base Case: An empty tree is complete Binary Tree
if (root == NULL)
return true;
else
{
// get the front node of the queue.
struct node* front = getFront(queue);
// If the left child of this front node doesnt exist, set the
// left child as the new node
if (!front->left)
front->left = temp;
// If the right child of this front node doesnt exist, set the
// right child as the new node
else if (!front->right)
front->right = temp;
// If the front node has both the left child and right child,
// Dequeue() it.
if (hasBothChild(front))
Dequeue(queue);
}
// Do something like normal level order traversal order. Following are the
// differences with normal level order traversal
// 1) Instead of printing a node, we push the node to stack
// 2) Right subtree is visited before left subtree
while (Q.empty() == false)
{
/* Dequeue node and make it root */
root = Q.front();
Q.pop();
S.push(root);
// Now pop all items from stack one by one and print them
while (S.empty() == false)
{
root = S.top();
cout << root->data << " ";
S.pop();
}
}
// Enqueue the only first level node and marker node for end of level
Q.push(root);
Q.push(marker);
// traversal loop
while (!q.empty())
{
// Pop two items from queue
first = q.front();
q.pop();
second = q.front();
q.pop();
Print extreme nodes of each level of Binary Tree in alternate order - GeeksforGeeks
http://www.geeksforgeeks.org/print-extreme-nodes-of-each-level-of-binary-tree-in-alternate-
order/
/* Function to print nodes of extreme corners
of each level in alternate order */
void printExtremeNodes(Node* root)
{
if (root == NULL)
return;
// Dequeue node
q.pop();
Convert a Binary Tree into Doubly Linked List in spiral fashion - GeeksforGeeks
http://www.geeksforgeeks.org/convert-a-binary-tree-into-doubly-linked-list-in-spiral-fashion/
int level = 0;
while (!q.empty())
{
// nodeCount indicates number of Nodes
// at current level.
int nodeCount = q.size();
nodeCount--;
}
}
else //even level
{
while (nodeCount > 0)
{
// dequeue node from the back & push it
// to stack
Node *node = q.back();
q.pop_back();
stk.push(node);
Find next node in same level for given node in a binary tree - Techie Delight
http://www.techiedelight.com/find-next-node-in-same-level-binary-tree/
Find next node in same level for given node
if (front->left)
queue.push_back(front->left);
if (front->right)
queue.push_back(front->right);
}
}
return nullptr;
}
if (curr->left)
queue.push_back(curr->left);
if (curr->right)
queue.push_back(curr->right);
}
}
}