Count all K-Sum Paths in a Binary Tree using JavaScript Last Updated : 23 Jul, 2025 Comments Improve Suggest changes Like Article Like Report Given a binary tree and a number k, our task is to find the total number of paths where the sum of the values along the path equals k. The path can start and end at any node in the tree and must go downwards means moving only from parent nodes to child nodes. ApproachDefine a class T_Node representing nodes in a binary tree. Each node has a value, left, and right child.The count_Paths_Fun function calculates the number of paths in the binary tree that sum to a given value k. It uses a Depth-First Search (DFS) approach to traverse the tree.Within the "dfs" function, the code recursively traverses the binary tree. It updates currentSum by adding the value of each node, keeping track of the current path sum.During traversal, it computes oldSum by subtracting k from currentSum to check for existing paths that sum to k. The pathCount is incremented by the count of paths with sum oldSum.After traversing the tree, display the number of paths in the tree that sum to k. Example: The example below shows how to count all k-sum paths in a Binary Tree using JavaScript. JavaScript class T_Node { constructor(value = 0, left = null, right = null) { this.value = value; this.left = left; this.right = right; } } function count_Paths_Fun(root, k) { let pathCount = 0; let prefixSums = { '0': 1 }; function dfs(node, currentSum) { if (!node) return; // Update the current sum with the value of the current node currentSum += node.value; let oldSum = currentSum - k; pathCount += (prefixSums[oldSum] || 0); prefixSums[currentSum] = (prefixSums[currentSum] || 0) + 1; dfs(node.left, currentSum); dfs(node.right, currentSum); prefixSums[currentSum] -= 1; } dfs(root, 0); return pathCount; } // Constructing the tree as shown in the diagram const root = new T_Node(10); root.left = new T_Node(5); root.right = new T_Node(-3); root.left.left = new T_Node(3); root.left.right = new T_Node(2); root.right.right = new T_Node(11); root.left.left.left = new T_Node(3); root.left.left.right = new T_Node(-2); root.left.right.right = new T_Node(1); const k = 8; const result = count_Paths_Fun(root, k); console.log(`Number of paths that sum to ${k}: ${result}`); OutputNumber of paths that sum to 8: 3 Time Complexity: O(N), where N is the number of nodes in the binary tree. Space Complexity: O(N). Create Quiz Comment S sachinparmar98134 Follow 0 Improve S sachinparmar98134 Follow 0 Improve Article Tags : JavaScript Web Technologies JavaScript-Program Explore JavaScript BasicsIntroduction to JavaScript4 min readVariables and Datatypes in JavaScript6 min readJavaScript Operators5 min readControl Statements in JavaScript4 min readArray & StringJavaScript Arrays7 min readJavaScript Array Methods7 min readJavaScript Strings5 min readJavaScript String Methods9 min readFunction & ObjectFunctions in JavaScript5 min readJavaScript Function Expression3 min readFunction Overloading in JavaScript4 min readObjects in JavaScript4 min readJavaScript Object Constructors4 min readOOPObject Oriented Programming in JavaScript3 min readClasses and Objects in JavaScript4 min readWhat Are Access Modifiers In JavaScript ?5 min readJavaScript Constructor Method7 min readAsynchronous JavaScriptAsynchronous JavaScript2 min readJavaScript Callbacks4 min readJavaScript Promise4 min readEvent Loop in JavaScript4 min readAsync and Await in JavaScript2 min readException HandlingJavascript Error and Exceptional Handling6 min readJavaScript Errors Throw and Try to Catch2 min readHow to create custom errors in JavaScript ?2 min readJavaScript TypeError - Invalid Array.prototype.sort argument1 min readDOMHTML DOM (Document Object Model)8 min readHow to select DOM Elements in JavaScript ?3 min readJavaScript Custom Events4 min readJavaScript addEventListener() with Examples9 min readAdvanced TopicsClosure in JavaScript4 min readJavaScript Hoisting6 min readScope of Variables in JavaScript3 min readJavaScript Higher Order Functions7 min readDebugging in JavaScript4 min read Like