Introduction
This is a simple yet non trivial problem. Given two trees we need to evaluate if one tree is the sub tree of the other. If the condition is satisfied, it returns true, else it returns false.
The definition of sub tree must not be ambiguous. We say that two trees are equal if they are structurally equal and their corresponding nodes have equal keys.
Equality of trees
Remember that subtree and contains relation are not the same. It may be possible that a tree T1 contains another tree T2 and still T2 is not a sub tree of T1.
This also means that all subtree satisfies the con...

Read More
# Author: dharam

## Minimum Range over K arrays

Introduction
This is one interesting problem with a non-trivial solution, this has been asked in couple of good interviews. Here goes the problem statement.
"Given K positive integer arrays, each of which contains elements in sorted order. Find a range of integers such that it must contain at least one element from each array and the range is minimum."
May be a diagram can make this more clear. Here you go!
Understanding the Minimum Range over K arrays problem.
In the above diagram we have 3 arrays each containing elements in increasing order. Now, let us identify a range which cont...

Read More
## Shortest path in Binary Search Tree

Introduction
Another interview question for the most interesting data structure called BST (Binary Search Tree). To know more about BSTs check my previous post.
Problem Statement : Given a Binary Search Tree, keyed on positive integers. The task is to find the Shortest path in Binary Search Tree which adds up to the number K. If no such path exists, return a message accordingly.
Understanding the problem
A node in a binary search tree always has a higher key at its right child compared to its left child. There are many path running from the roots to the leaves. When we add up the key val...

Read More
## Dynamic Programming – Longest Palindromic Sequence

[nextpage title="Introduction"]
Palindromes are fascinating character sequences in a string. A palindrome is a string which reads the same when read from either of the ends. This post in particular talks about palindromic sub sequences.
To know more about a sub sequence, please check my post on Longest Common Sub sequence.
A palindromic sequence means a sequence of characters which is a palindrome. Now, we must understand it clearly that we are talking about a sub sequence and not a substring.
Understanding the Longest Palindromic Subsequence problem better
It is really easy to say if...

Read More
## Binary Tree Linking Neighbors

Introduction
Problem Statement : Given a regular binary tree with left, right and peer node pointers. The left and the right pointers are already populated. We need to make the peer pointer point to the next right neighbor on the same level.
Understanding the problem
Here is a diagram to explain what is needed. The red peer pointer points to the immediate right neighbor if the neighbor exists.
Approach 1
The first solution which comes in mind is to do a custom, level order traversal as done in the post. And, in each iteration of the inner loop, just link each of the node to the next in ...

Read More
## Linked List Batch Reversing

Introduction
As I am writing this at 2 AM, this is going to be a small post but definitely an interesting one. This is mostly asked in interviews and is related to my previous post for Reversing a LinkedList.
Understanding the problem statement
Given a singly linked list L and a number k, we need to reverse each batch of k elements in the linked list. Here is a pictorial representation for the same.
For the below input linked list and for k equals to 3, you can see the output linked list under it. Each of the triplets are reversed.
Approach
We need to use our reverse linked list func...

Read More
## Topological Ordering for Graphs

Introduction
The topological ordering for graphs get many applications where the nature of the problem is ordered sequential processing. Few of the problems in this category are as follows:
Dynamic Linking/Loading of programs while building and execution.
Deciding the pre-requisites in a course structure.
Job Scheduling in Processors or Assembly Line Processing.
Building an Alien Dictionary from given words.
There are many more but I am sure you got the idea. When we say topological ordering of graph, we are necessarily talking about the ordering of the vertices of the graph.
Un...

Read More