Write a C program to compute the maximum depth in a tree?
Here is some C code…
int maxDepth(struct node* node)
{
if (node==NULL)
{
return(0);
}
else
{
int leftDepth = maxDepth(node->left);
int rightDepth = maxDepth(node->right);
if (leftDepth > rightDepth) return(leftDepth+1);
else return(rightDepth+1);
}
}
Write a C program to find the mininum value in a binary search tree.
Here is some sample C code. The idea is to keep on moving till you hit the left most node in the tree
int minValue(struct node* node)
{
struct node* current = node;
while (current->left != NULL)
{
current = current->left;
}
return(current->data);
}
On similar lines, to find […]
Write C code to determine if two trees are identical
Here is a C program using recursion
int identical(struct node* a, struct node* b)
{
if (a==NULL && b==NULL){return(true);}
else if (a!=NULL && b!=NULL)
{
return(a->data == b->data &&
identical(a->left, b->left) &&
[…]
Write a C program to determine the number of elements (or size) in a tree.
Try this C program
int tree_size(struct node* node)
{
if (node==NULL)
{
return(0);
}
else
{
return(tree_size(node->left) + tree_size(node->right) + 1);
}
}
Write a C program to find the depth or height of a tree.
Here is some C code to get the height of the three
tree_height(mynode *p)
{
if(p==NULL)return(0);
if(p->left){h1=tree_height(p->left);}
if(p=>right){h2=tree_height(p->right);}
return(max(h1,h2)+1);
}
The degree of the leaf is zero. The degree of a tree is the max of its element degrees. A binary tree of height n, h > 0, has at […]
How would you detect a loop in a linked list? Write a C program to detect a loop in a linked list.
This is also one of the classic interview questions
There are multiple answers to this problem. Here are a few C programs to attack this problem.
Brute force method
Have a double loop, where you check the node pointed to by the outer loop, with every node of the inner loop.
typedef struct node
{
void *data;
struct […]
How do you reverse a linked list without using any C pointers?
One way is to reverse the data in the nodes without changing the pointers themselves. One can also create a new linked list which is the reverse of the original linked list. A simple C program can do that for you. Please note that you would still use the “next” pointer fields to traverse through […]
Read the rest of this entry »How do you sort a linked list? Write a C program to sort a linked list.
This is a very popular interview question, which most people go wrong. The ideal solution to this problem is to keep the linked list sorted as you build it. Another question on this website teaches you how to insert elements into a linked list in the sorted order. This really saves a lot of time […]
Read the rest of this entry »Given only a pointer to a node to be deleted in a singly linked list, how do you delete it?
This is a very good interview question
The solution to this is to copy the data from the next node into this node and delete the next node!. Ofcourse this wont work if the node to be deleted is the last node. Mark it as dummy in that case. If you have a Circular linked list, […]
What is the difference between malloc() and calloc()?
First lets look at the prototypes of these two popular functions..
#include
void *calloc(size_t n, size_t size);
void *malloc(size_t size);
The two functions malloc() and calloc() are functionally same in that they both allocate memory from a storage pool (generally called heap). Actually, the right thing to say is that these two […]

