Path Sum

easy
1. You are given the root of a binary tree containing N nodes and a sum.
 2. You have to find whether the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.
 3. For example,
 Input: [1,2,3,4,5,6,7] and sum = 8
 
 Output: Yes
 
 Explanation: The given binary tree is:
   
            1           
          /    
         2     3         
        /    /  
       4   5 6   7       
 
 There exists a root-to-leaf path (1=>2=>5) whose sum of nodes is equal to given sum = 8
 
 Notes:
 1. TreeNode class represents the node of a Binary tree.
 2. null in the given input string denotes no node.
 3. display is a utility function which displays the contents of tree, feel free to use it for debugging purposes.
 4. main takes input from the users and creates the tree. You can use display to know its contents.
 5. This is a functional problem.
 6. You have to complete the function hasPathSum. It takes as input the root node of the given tree and the given sum. It should return true if the tree has required path, else should return false.
 7. Don't change the code of TreeNode, main, display and other utility functions.

Input Format

First line takes input string representing the elements of the tree. Second line takes input sum. Input is handled for you.

Output Format

Yes or No. Output is handled for you.

Constraints

N/A

Notice

Try First, Check Solution later

1. You should first read the question and watch the question video.
2. Think of a solution approach, then try and submit the question on editor tab.
3. We strongly advise you to watch the solution video for prescribed approach.

Example

Input
1 2 3 4 5 6 7 
8
Output
Yes
Next
Subtree Sum

Related Questions