AfterAcademy Tech
•
01 May 2020

Difficulty: Easy
Asked in: Amazon, Microsoft, Yahoo
Given a binary tree and a sum, write a program to determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.
Problem Note
Example 1
Input: Given the below binary tree and sum = 22
5
/ \
4 8
/ / \
11 13 4
/ \ \
7 2 1
Output: true
Explanation: There exist a root-to-leaf path 5->4->11->2 which sum is 22.
The node structure passed to your function will be
class TreeNode
{
int val
TreeNode left
TreeNode right
}
You may try to solve the problem here: Path sum in a binary tree
Learning via problem-solving is the best way to crack any interview!
This problem could be solved by making a simple DFS or preorder traversal through the tree. If you will think that, all we have to do is to check if any path from the root to any leaf will sum up to the given sum.
Example →

Let's consider the above example, there are many paths like 5->4->11->7 or 5->4->11->2 and so on. This seems like a simple preorder traversal in which we will perform some check on the root node, then recursively go to the left subtree and after checking all the left subtree, we go to the right subtree.
In this case, If we will try to think of a similar way of traversal with passing another argument, i.e. sum with every level of traversal, its value decreases by the root value of that subtree, then all we have to check if the leaf node is equal to the sum at that state, If that is so, then return True otherwise return False.
Solution Steps
root is null return falserootnode.val from the given sum to get new sum3. if the root node visited is a leaf node and its value is equal to the new sum then return true
Pseudo Code
int pathSumExists(TreeNode root, int sum) {
if(root is NULL)
{
return false
}
if(root.val == sum and root.left is NULL and root.right is NULL)
{
return true
}
return pathSumExists(root.left, sum - root.val) or pathSumExists(root.right, sum - root.val)
}
Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(n) (How?)
Critical Ideas to Think
If you understood how the recursive approach is working behind the hood, then you can clone the similar procedure into the iteration.
There are two ways in which you can do that
Solution Step
sum , then return True.Pseudo Code
Using Two Queues
bool pathSumExists(TreeNode root, int sum) {
if(root is null)
return false
// Queue of tree node
Queue nodes
// Queue of Integers
Queue values
nodes.add(root)
values.add(root.val)
while(nodes is not empty) {
TreeNode curr = nodes.front()
nodes.pop()
sumValue = values.front()
values.pop()
if(curr.left is null and curr.right is null and sumValue==sum){
return true
}
if(curr.left is not null){
nodes.add(curr.left)
values.add(sumValue + curr.left.val)
}
if(curr.right is not null){
nodes.add(curr.right)
values.add(sumValue+curr.right.val)
}
}
return false
}
By modifying the tree nodes.
bool pathSumExists(TreeNode root, int sum){
if(root is NULL)
return false
// Queue of treenode
queue nodes
nodes.add(root)
while(nodes is not empty){
TreeNode curr = nodes.front()
nodes.pop()
if(curr.left is NULL and curr.right is NULL){
if(curr.val == sum)
return true
}
if(curr.left){
curr.left.val = curr.left.val + curr.val
nodes.push(curr.left)
}
if(curr.right){
curr.right.val = curr.right.val + curr.val
nodes.push(curr.right)
}
}
return false
}
Complexity Analysis
Time Complexity: O(N)
Space Complexity: O(N)
Critical Ideas to Think

If you have any more approaches or you find an error/bug in the above solutions, please comment down below.
Happy Coding! Enjoy Algorithms!
AfterAcademy Tech
Given a non-empty binary tree, find maximum path sum. For this problem, a path is defined as any sequence of nodes from some starting node to any node in the tree along with the parent-child connections.

AfterAcademy Tech
Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a binary tree is the length of the longest path between any two nodes in a tree. This path may or may not pass through the root.

AfterAcademy Tech
Given a binary tree, write a program to find the maximum depth of the binary tree. The maximum depth is the number of nodes along the longest path from the root node to the leaf node. A leaf is a node with no child nodes.

AfterAcademy Tech
Given a binary tree, invert the binary tree and return it. An inverted form of a Binary Tree is another Binary Tree with left and right children of all non-leaf nodes interchanged. You may also call it the mirror of the input tree.
