Close

## Left view of a binary tree

Given a binary tree, how do we print the left view of it?For example, consider the simple binary tree below    1 / 2   3 The left view of this is 1, 2. Similarly consider one more example             1          /           2    3             /             4   5Left view for this tree contains 1, 2,…

## Longest path in a binary tree

Given a binary tree, how do we find the longest distance between any two nodes in it? Some times this is referred to as the diameter of the tree.For example consider the below binary tree The longest path in this tree is 1 – 2 – 3 – 5 – 8 – 6 – 7.…

## Finding the longest consecutive subset

Given an array of positive and negative numbers(distinct), How do we find the length of the longest consecutive subset? For example consider the below array {3, 6, 1, 2, 5, 7, 8} It contains {5,6,7,8} as the longest consecutive subset. So the answer is 4. Similarly for {2, 4, 6, 8}, the answer is 1…

## Number of characters appearing in all given strings

Given a set of n strings, we have to write a program to find out the number of characters appearing in all the strings.For example consider the following strings{“India”, “China”, “United states”}, the letters {a,i,n} appear in all the strings.Let us think of the following solution. We first add all the characters in the first…

## Reversing a linked list in groups of given size

Given a single linked list, and an integer k, we need to reverse the linked list in groups of size k. For example consider the following diagram of input and output linked lists for k = 3. There are no tricks involved in the solution for this problem. It is just an implementation challenge where…

## Building a binary tree using inorder and post/pre order traversals

Given the in-order, and pre-order or post-order sequences of a binary tree, how do we construct a binary tree from them?Given only pre-order and post order traversals, we can not construct a unique binary tree from them.For example given In-order : {1, 2, 3, 4, 5, 6, 7, 8}Pre-order: {5, 3, 2, 1, 4, 8, 6,…

## Finding the number of duplicates in a list of numbers

Given a list of numbers, we have to find how many duplicate entries are present in it? For example consider the list of numbers {5, 1, 9, 5, 2, 1, 6, 4, 5, 8}It has 3 duplicate numbers. There are many ways to do this. In this post, I present one of the simplest ways…

## Counting the number of leaf nodes in a binary tree

Given a binary tree, write a program to count the number of leaf nodes. A leaf node is a node which does not have left and right children.For example consider the following binary tree. It has 3 leaf nodes namely 1, 4 and 7. The solution is simple. The number of leaf nodes of a…

## Deleting duplicate elements from a sorted linked list

Given a sorted linked list, how do we delete duplicate nodes from it?This question is simple and straightforward, we take two pointers, current and next which points the adjacent nodes. In each iteration we compare the values at these two nodes, if they are equal, we delete the node pointed by next and advance the…

## Splitting the linked list into two halves

Given a single linked list, how do we split it into two halves. For example the list 1->2->3->4 should be divided into 1->2 and 3->4 If the length of the list is odd, add the extra node to first half; so 1->2->3 should be split to 1->2 and 3. This problem can be efficiently solved…