Path Sum (Binary Tree Root to Leaf Path Sum)

Binary Tree Root to Leaf Path Sum Equal to a Given Number.

Given a binary tree and a sum, Determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.

If path is found then return true else return false.

Note: What is leaf node? Any node whose left and right children is null is known as leaf node.

For example –

In this Binary tree, there exist a path from root-to-leaf node whose sum is equal to the given sum (which is 16).

Now, suppose if the given value of sum is 12. Then in this case we simply return false. As the root-to-leaf node path does not exist in a Binary tree whose sum is equal to 12.

Minimum Size Subarray Sum

Minimum Size Subarray Sum (Smallest subarray whose sum is greater than or equal to target).

Given an array of positive integers and a positive number k. Find the smallest contiguous subarray whose sum is either greater than or equal to k. If no subarray is found then return 0.

For example –

Example 1 –

  Input :  {7, 2, 1, 1, 6, 5},  k = 11

Output:  2 ( subarray {6, 5} has the minimum length )

Example 2 –

  Input : {1, 4, 3},   k = 12

Output: 0 (No subarray is possible)

Merge Overlapping Intervals

In this post, we are going to solve a problem merge overlapping intervals.

Given a list of time intervals. Each interval has a start and end time. Write a code to merge all overlapping intervals. The given intervals may or may not be sorted.

For example –

Example 1-

Input: [[1,4], [2,5], [6,9]] Output: [[1,5], [6,9]]

Example 2-

Input: [[7,8], [2,3], [5,9]] Output: [[2,3], [5,9]]

Example 3-

Input: [[1,4], [2,6], [3,5]] Output: [[1,6]]