This process when implemented iteratively also requires a stack and a boolean to prevent the execution from traversing any portion of a tree twice. The general process of in-order traversal follows the following steps:
Create an empty stack S. Initialize current node as root Push the current node to S and set current = current->left until current is NULL If current is NULL and stack is not empty then
[Read More]
DFS (Depth First Search) on tree or graph
Consider this tree (which is a graph without a cycle) :
Now DFS means traversing to the depth…and then going up, so here DFS traversal will result in 5 2 -4 3 21 19 25. We can use non recursive as well recursive approach to solve this problem.
A depth first search (DFS) through a tree starts at the root and goes straight down a single branch until a leaf is reached.
[Read More]
BFS (Breadth first search ) OR Level Order Traversal on tree
Algorithm
Starting at some arbitrarily chosen vertex s (s stands for start vertex) , we mark v so that we know we’ve visited it, process v, and then visit i.e. mark the vertex as visited and process all of v’s neighbors. Now that we’ve visited and processed all of v’s neighbors, we need to visit and process all of v’s neighbors neighbors Example
So consider the tree:
[Read More]
Check if Tree is a Balanced Tree
Problem Implement a function to check if a tree is balanced. For the purposes of this question, a balanced tree is defined to be a tree such that no two leaf nodes differ in distance from the root by more than one
Solution Method 1 - Difference of height at each node should not be greater than 1
Here is the code :
public boolean isBalanced(Node root){ if(root==null){ return true; //tree is empty } else{ int lh = root.
[Read More]
Print a Binary Tree in Zig Zag Level Order or spiral order
Question: Print a binary tree in zig-zag level order. Zig-zag means print 1st level from left to right, then 2nd level right to left and alternate thereafter.
Example: zig-zag level order traversal of below tree will be
0 2 1 3 4 5 6 14 13 12 11 10 9 8 7
Solution1 - Iterative solution
Answer: We usually use a queue for level order traversal of a queue. If we want to use the same here, how will we change direction of traversal on every alternate level?
[Read More]
Find Diameter of Binary Tree
the complexity is O(n^2) . since each node is visi…
Unknown - May 1, 2013
the complexity is O(n^2) . since each node is visit multiple times as the traversal is top bottom and not bottom up.
Find Diameter of Binary Tree
So this question asks us to find Number of Nodes on the Longest Path in Binary Tree so one thing is sure that this path comprises on two leaves with maximum distance in BT..isn’t it
PS:Don’t confused with finding the maximum distance between two nodes in Binary Tree(as it doesn’t involves 2 leaves)
The diameter of a tree (sometimes called the width) is the number of nodes on the longest path between two leaves in the tree.
[Read More]
Duplicate removal in Binary Search Tree
#include <stdio.h\> class BinarySearchTree { private: int data; BinarySearchTree \*left, \*right; public: BinarySearchTree(const int d):data(d), left(NULL), right(NULL){} ~BinarySearchTree() { if(left !\= NULL) { delete left; } if(right !\= NULL) { delete right; } } //remove duplicates void removeDup() { if(left) { left\-\>removeDup(); left\-\>remove(data, this); } if(right) right\-\>removeDup(); } void remove(int value, BinarySearchTree \*parent) { if(value < data && left) { left\-\>remove(value, this); } else if(value \> data && right) { right\-\>remove(value, this); } else remove(parent); } void remove(BinarySearchTree \*parent) //remove this node { if(left \=\= NULL && right \=\= NULL) //leaf { ((parent\-\>left \=\= this) ?
[Read More]
Given a binary tree, print the sum( inclusive of the root ) of sub-tree rooted at each node in in-order
Given a binary tree. Find the sum( inclusive of the root ) of sub-tree rooted at each node. Print the sum in in-order.
E.g.
3
/ \\
2 4
/ / \\
2 2 -1
Output: 2, 4, 12, 2, 5, -1.
Solution
#include <stdio.h>
#include <stdlib.h>
typedef struct TreeNode
{
struct TreeNode \*left, \*right;
int data;
}TreeNode;
typedef TreeNode \* Tree;
/\*
Two passes with constant space
\*/
int printSum(Tree t, int toPrint)
{
if(t == NULL)
return 0;
int lsum = printSum(t->left, toPrint);
int rsum = printSum(t->right, 0);
int sum = lsum + t->data + rsum;
if(toPrint)
printf("%d ", sum);
printSum(t->right, toPrint);
return sum;
}
/\*
One pass with n space
\*/
int printSum2(Tree t, int a\[\], int \*pos)
{
if(t == NULL)
return 0;
int lsum = printSum2(t->left, a, pos);
(\*pos)++;
int p = \*pos;
int rsum = printSum2(t->right,a, pos);
return a\[p\] = lsum + t->data + rsum;
}
/\*Utilities\*/
inline TreeNode \* makeTreeNode(int data)
{
TreeNode \*n = calloc(sizeof(TreeNode), 1);
n->data = data;
return n;
}
int main()
{
/\*level 0\*/
Tree t = makeTreeNode(3);
/\*level 1\*/
t->left = makeTreeNode(2);
t->right = makeTreeNode(4);
/\*level 2\*/
t->left->left = makeTreeNode(2);
t->right->left = makeTreeNode(2);
t->right->right = makeTreeNode(-1);
/\*print sum\*/
printSum(t, 1);
printf("\\n");
int a\[100\];
int pos = -1;
printSum2(t, a, &pos);
int i;
for(i = 0; i <= pos; ++i) {
printf("%d ", a\[i\]);
}
printf("\\n");
return 0;
}
Given a binary tree, print the sum( inclusive of the root ) of sub-tree rooted at each node in in-order
Code :
#include <stdio.h>
#include <stdlib.h>
typedef struct TreeNode
{
struct TreeNode \*left, \*right;
int data;
}TreeNode;
typedef TreeNode \* Tree;
/\*
Two passes with constant space
\*/
int printSum(Tree t, int toPrint)
{
if(t == NULL)
return 0;
int lsum = printSum(t->left, toPrint);
int rsum = printSum(t->right, 0);
int sum = lsum + t->data + rsum;
if(toPrint)
printf("%d ", sum);
printSum(t->right, toPrint);
return sum;
}
/\*
One pass with n space
\*/
int printSum2(Tree t, int a\[\], int \*pos)
{
if(t == NULL)
return 0;
int lsum = printSum2(t->left, a, pos);
(\*pos)++;
int p = \*pos;
int rsum = printSum2(t->right,a, pos);
return a\[p\] = lsum + t->data + rsum;
}
/\*Utilities\*/
inline TreeNode \* makeTreeNode(int data)
{
TreeNode \*n = calloc(sizeof(TreeNode), 1);
n->data = data;
return n;
}
int main()
{
/\*level 0\*/
Tree t = makeTreeNode(3);
/\*level 1\*/
t->left = makeTreeNode(2);
t->right = makeTreeNode(4);
/\*level 2\*/
t->left->left = makeTreeNode(2);
t->right->left = makeTreeNode(2);
t->right->right = makeTreeNode(-1);
/\*print sum\*/
printSum(t, 1);
printf("\\n");
int a\[100\];
int pos = -1;
printSum2(t, a, &pos);
int i;
for(i = 0; i <= pos; ++i) {
printf("%d ", a\[i\]);
}
printf("\\n");
return 0;
}