Path Sum

5

2 votes
Easy
Problem

Given n number each representing value of a node, construct a tree in the following manner:

  1. Let's say node to be inserted has value x
  2. If x is less than or equal to value of the current root node then  insert to the left of the current root
  3. If x is greater than the value of the current root then insert node in the right of this node

The above insertion will make a special tree known as Binary Search Tree.

Now given q queries, each query containing a sum find whether there exist a path from root to leaf with sum equal to the given sum.

Input Format:

First line of input contains an integer n i.e. number of nodes. Second line contains n space separate integers. Third line contains an integer q. Followed by q integers.

Output Format:

Print "Yes" if there exist a path equal to sum else print no.

Constraints:

All the numbers are well with in the integer range.

Time Limit: 5
Memory Limit: 256
Source Limit:
Editor Image

?