Root to leaf path sum equal to a given number in BST
Last Updated : 11 Jul, 2025
Given a Binary Search Tree and a sum target. The task is to check whether the given sum is equal to the sum of all the node from root leaf across any of the root to leaf paths in the given Binary Search Tree.
Examples:
Input:
Output: true Explanation: root-to-leaf path [1, 3, 4] sums up to the target value 8.
Input:
Output: false Explanation: There is no root-to-leaf path that sums up to the target value 9.
Approach:
The idea is to recursively traverse the binary tree, accumulating the sum of node values along the path. At each node, we check if we have reached a leaf node and if the accumulated sum matches the target. If the sum matches at any leaf node, we return true. If not, we continue checking both the left and right subtrees until we either find a valid path or exhaust the tree.
C++
// C++ program to check if a root-to-leaf path exists // with the given sum in a Binary Search Tree#include<bits/stdc++.h>usingnamespacestd;classNode{public:intdata;Node*left;Node*right;Node(intx){data=x;left=right=nullptr;}};// Function to check if a root-to-leaf path has // the target sumboolhasPathSum(Node*curr,inttarget,intcurrsum){if(curr==nullptr){returnfalse;}// Add current node's value to the running sumcurrsum+=curr->data;// Check if it's a leaf node and the sum equals // the targetif(curr->left==nullptr&&curr->right==nullptr){returncurrsum==target;}// Recursively check left and right subtreesreturnhasPathSum(curr->left,target,currsum)||hasPathSum(curr->right,target,currsum);}// Wrapper function to start the check from rootboolcheckPathSum(Node*root,inttarget){returnhasPathSum(root,target,0);}intmain(){// Representation of given Binary Search Tree// 1// / \ // 9 3// / \ // 4 7Node*root=newNode(1);root->left=newNode(9);root->right=newNode(3);root->right->left=newNode(4);root->right->right=newNode(7);inttarget=8;if(checkPathSum(root,target)){cout<<"true"<<endl;}else{cout<<"false"<<endl;}return0;}
Java
// Java program to check if a root-to-leaf path exists // with the given sum in a Binary Search Treeimportjava.util.*;classNode{intdata;Nodeleft,right;Node(intx){data=x;left=right=null;}}classGfG{// Utility function to check root-to-leaf path sumstaticbooleanhasPathSum(Nodecurr,inttarget,intcurrsum){if(curr==null){returnfalse;}// Add current node's value to the running sumcurrsum+=curr.data;// Check if it's a leaf node and the sum equals targetif(curr.left==null&&curr.right==null){returncurrsum==target;}// Recursively check left and right subtreesreturnhasPathSum(curr.left,target,currsum)||hasPathSum(curr.right,target,currsum);}// Wrapper function to start the check from rootstaticbooleancheckPathSum(Noderoot,inttarget){returnhasPathSum(root,target,0);}publicstaticvoidmain(String[]args){// Representation of given Binary Search Tree// 1// / \// 9 3// / \// 4 7Noderoot=newNode(1);root.left=newNode(9);root.right=newNode(3);root.right.left=newNode(4);root.right.right=newNode(7);inttarget=8;if(checkPathSum(root,target)){System.out.println("true");}else{System.out.println("false");}}}
Python
# Python program to check if a root-to-leaf path # exists with the given sum in a Binary TreeclassNode:def__init__(self,data):self.data=dataself.left=Noneself.right=None# Function to check root-to-leaf path sumdefhasPathSum(curr,targetSum,currSum):ifcurrisNone:returnFalse# Add current node's value to the running sumcurrSum+=curr.data# Check if it's a leaf node and the sum equals targetifcurr.leftisNoneandcurr.rightisNone:returncurrSum==targetSum# Recursively check left and right subtreesreturn(hasPathSum(curr.left,targetSum,currSum)orhasPathSum(curr.right,targetSum,currSum))# Wrapper function to start the check from rootdefcheckPathSum(root,targetSum):returnhasPathSum(root,targetSum,0)if__name__=='__main__':# Representation of the binary tree# 1# / \# 9 3# / \# 4 7root=Node(1)root.left=Node(9)root.right=Node(3)root.right.left=Node(4)root.right.right=Node(7)targetSum=8ifcheckPathSum(root,targetSum):print("true")else:print("false")
C#
// C# program to check if a root-to-leaf path// exists with the given sum in a Binary TreeusingSystem;classNode{publicintdata;publicNodeleft,right;publicNode(intx){data=x;left=right=null;}}classGfG{// Function to check root-to-leaf path sumstaticboolHasPathSum(Nodecurr,inttarget,intcurrSum){if(curr==null){returnfalse;}// Add current node's value to the running sumcurrSum+=curr.data;// Check if it's a leaf node and sum matches targetif(curr.left==null&&curr.right==null){returncurrSum==target;}// Recursively check left and right subtreesreturnHasPathSum(curr.left,target,currSum)||HasPathSum(curr.right,target,currSum);}// Wrapper function to start from rootstaticboolCheckPathSum(Noderoot,inttarget){returnHasPathSum(root,target,0);}staticvoidMain(string[]args){// Representation of given Binary Tree// 1// / \// 9 3// / \// 4 7Noderoot=newNode(1);root.left=newNode(9);root.right=newNode(3);root.right.left=newNode(4);root.right.right=newNode(7);inttarget=7;if(CheckPathSum(root,target)){Console.WriteLine("true");}else{Console.WriteLine("false");}}}
JavaScript
// JavaScript program to check if a root-to-leaf// path exists with the given sum in a Binary TreeclassNode{constructor(data){this.data=data;this.left=null;this.right=null;}}// Function to check root-to-leaf path sumfunctionhasPathSum(curr,target,currSum){if(curr===null){returnfalse;}// Add current node's value to the running sumcurrSum+=curr.data;// Check if it's a leaf node and sum matches targetif(curr.left===null&&curr.right===null){returncurrSum===target;}// Recursively check left and right subtreesreturnhasPathSum(curr.left,target,currSum)||hasPathSum(curr.right,target,currSum);}// Wrapper function to start from rootfunctioncheckPathSum(root,target){returnhasPathSum(root,target,0);}// Representation of given Binary Tree// 1// / \// 9 3// / \// 4 7constroot=newNode(1);root.left=newNode(9);root.right=newNode(3);root.right.left=newNode(4);root.right.right=newNode(7);consttarget=8;if(checkPathSum(root,target)){console.log("true");}else{console.log("false");}
Output
true
Time Complexity: O(n), where n is the number of nodes in the binary tree. Auxiliary Space: O(h), where h is the height of the tree.