VOOZH about

URL: https://www.geeksforgeeks.org/dsa/root-to-leaf-path-sum-equal-to-a-given-number/

⇱ Root to leaf path sum equal to a given number - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Root to leaf path sum equal to a given number

Last Updated : 23 Jul, 2025

Given a binary tree and a sum, return true if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. Return false if no such path can be found. 

Example:

Input:

👁 Root-to-leaf-path-sum-equal-to-a-given-number

Output: True 
Explanation:  Root to leaf path sum, existing in this tree are:

  • 10 -> 8 -> 3  = 21
  • 10 -> 8 -> 5  = 23
  • 10 -> 2 -> 2 = 14

So it is possible to get sum = 21

[Expected Approach - 1] Using Recursion - O(n) Time and O(h) Space

The idea is to recursively move to left and right subtree and decrease sum by the value of the current node and if at any point the current node is equal to a leaf node and remaining sum is equal to zero then the answer is true.

Follow the given steps to solve the problem using the above approach:

  • Recursively move to the left and right subtree and at each call decrease the sum by the value of the current node.
  • If at any level the current node is a leaf node and the remaining sum is equal to zero then return true.

Below is the implementation of the above approach:


Output
True

Time Complexity: O(n), where n is the number of nodes.
Auxiliary Space: O(h), where h is the height of the tree.

[Expected Approach - 2] Using Iterative - O(n) Time and O(h) Space

In this approach, we use a stack to perform a preorder traversal of the binary tree. We maintain two stacks - one to store the nodes and another to store the sum of values along the path to that node. Whenever we encounter a leaf node, we check if the sum matches the target sum. If it does, we return true, otherwise, we continue traversing the tree.

Follow the given steps to solve the problem using the above approach:

  • Check if the root node is NULL. If it is, return false, since there is no path to follow.
  • Create two stacks, one for the nodes and one for the sums. Push the root node onto the node stack and its data onto the sum stack. While the node stack is not empty, do the following:
    • Pop a node from the node stack and its corresponding sum from the sum stack.
    • Check if the node is a leaf node (i.e., it has no left or right child). If it is, check if the sum equals the target sum. If it does, return true, since we have found a path that adds up to the target sum.
    • If the node has a left child, push it onto the node stack and push the sum plus the left child’s data onto the sum stack.
    • If the node has a right child, push it onto the node stack and push the sum plus the right child’s data onto the sum stack.
  • If we reach this point, it means we have exhausted all paths and haven’t found any that add up to the target sum. Return false.

Below is the implementation of the above approach:


Output
True

Time Complexity: O(n) where n is the number of nodes.
Auxiliary Space: O(h) where h is the height of the tree.

Comment