Path sum in binary tree

TopicDifficultyCompanies
Binary Tree
EASY
Amazon
Microsoft
Yahoo

Given the root node of a binary tree and a sum, write a program to determine if there exists a path in the tree from root-to-leaf such that the sum of all the values along the path equals the given sum.

Problem Note

  • A leaf is a node with no children.

Example 1

Input: Given the below binary tree and sum = 22
6
/ \
4 8
/ / \
10 14 3
/ \ \
8 2 1
Output: 1
Explanation: There exist a root-to-leaf path 6->4->10->2 which sum is 22 so output is true i.e. 1

Code Editor

Practice and Learn

Best way to learn is through solving real problems. Practice this problem in this code editor.