Tag Archives: Data Structures

Write C code to count the number of leaves in a tree

leaf nodes in binary search tree

In a binary search tree, a node which contains blank left and right children’s that particular node is called as a leaf node in a tree. Here We are going give a c program for finding the number of leaf nodes in a tree. In a c Program we explained recursively how to find leaf Read More →

Write a function to get the intersection point of two Linked Lists (Y Shape)

Method 1 (Brute Force) Using 2 for loops. Outer loop will be for each node of the 1st list and inner loop will be for 2nd list. In the inner loop, check if any of nodes of 2nd list is same as the current node of first linked list. Time complexity of this method will Read More →

Write a program to detect loop in a Linked List

linkedlistLoop

Floyd’s Algorithm: This problem can be solved by using two pointers, slow pointer and fast pointer.Both these pointers will point to head of the linked list initially. Increment slow pointer by 1 node and the fast pointer by 2 nodes. If there’s a loop, the fast pointer will meet the slow pointer somewhere.We’ll discuss in Read More →

Iterative preorder, inorder and postorder tree traversals

tree traversals pre order, post prder, in order

Here is a complete C program which prints a BST using both recursion and iteration. The best way to understand these algorithms is to get a pen and a paper and trace out the traversals (with the stack or the queue) alongside. Dont even try to memorize these algorithms! And here is the output… Creating the root.. Read More →

Shifting zeros to end in an array

Given an array with sorted numbers and zeros in between them, than how could we rearrange the array such that all the zeros are placed at end in O(1) space complexity. ex: I/p: 3 0 0 4 0 5 0 6 7 8 0 0 9 o/p: 3 4 5 6 7 8 9 0 Read More →

Print Level Order Traversal of a tree

levelordertraversal

Level Order Traversal of this tree would be 1 2 3 4 5 6 Approach: Starting from the root, first the node is visited and printed then it’s child nodes are put in a queue. Time Complexity: As we are visiting each node once.So the time complexity would be O(n) where n is the number Read More →

Determine if two binary trees are identical

Two trees are identical when they have same data and there physical layout  is also same. To identify if two trees are identical, we need to traverse both trees simultaneously, and while traversing we need to compare data and children of the trees. Time Complexity: Complexity of the problem will be according to the tree Read More →

Write C code to implement the Binary Search algorithm

Binary search is used to quickly find a value in a sorted sequence.At each step, the algorithm compares the median value in the search space to the target value. Based on the comparison and because the sequence is sorted, it can then eliminate half of the search space. By doing this repeatedly, it will eventually Read More →

Insert nodes into a linked list in a sorted fashion

The solution is to iterate down the list looking for the correct place to insert the new node. That could be the end of the list, or a point just before a node which is larger than the new node. Note that we assume the memory for the new node has already been allocated and Read More →

Write a C function to find the height or depth of a tree.

Write a C function to find the height or depth of a tree. Solution : The height can be found out recursively by calculating height of left sub tree tree and right sub tree.Height of tree would be the maximum  of height of left subtree and height of right subtree + 1.  

Write a C program to return the nth node from the end of a linked list

Solution 01 – Brute Force Approach: Let’s assume length of linked list be ‘L’ , nth node from end = L – n + 1 node from start. Step 1: Iterate over the linked list and determine its length. Step 2: Now using the above mentioned formula , we can determine the position of the Read More →

Reverse a string word by word,(in place)

Write a Program to reverse the words in a string. Example Input : Hello! How are you Output: you are How Hello! The words are reversed, but the letters are still in order (within the word). Algorithm: i)Reverse the whole string. You will get “uoy era woH !olleH” ii)Then reverse each word. Now it will Read More →