Count all K-Sum Paths in a Binary Tree using JavaScript Last Updated : 16 May, 2024 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). Comment More infoAdvertise with us Next Article Count all K-Sum Paths in a Binary Tree using JavaScript sachinparmar98134 Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Program Similar Reads Kth Ancestor in Binary Tree using JavaScript Given a binary tree and a node in this tree, our task is to find the k-th ancestor of the specified node in JavaScript. If no such ancestor exists, or when k exceeds the depth of the node, the function should return null. Example: Input: Node = 5, k = 2Output: 1ApproachFirst, calculate the depth of 2 min read Sum of Left leaves in Binary Tree using JavaScript Calculating the sum of all left leaves in a binary tree is a common task that helps in understanding tree traversal techniques. A left leaf is defined as a leaf node that is the left child of its parent node. This problem can serve various purposes, such as evaluating symmetry in data structures or 5 min read Insert a Node in Binary tree using JavaScript A binary tree is a tree data structure where each node has at most two children, referred to as the left child and the right child. The topmost node is called the root. Nodes can have values or data, and the left child's value is less than the parent's value, while the right child's value is greater 3 min read Kth Largest/Smallest Element in Binary Search Tree using JavaScript A binary search tree is a type of binary tree in which each node has two children: left child & right child. In a Binary search tree (BST) all nodes in the left subtree have values less than the node value and all nodes in the right subtree have values greater than the node value. Different appr 4 min read Find a Pair with a Given Sum in BST using JavaScript Given a target sum and Binary Search Tree (BST), we have to write a function to find out the pair of nodes in BST whose values will sum up to this given sum. We must give these two numbers as an array that adds up to get this target sum. If there is no such number pair, return null. Example: Input: 4 min read Height of Binary Tree using JavaScript The height of a binary tree can be defined as the maximum depth or length of the longest path from the root node to any leaf node. Below is an example to illustrate the height of the binary tree. Height of Binary tree is 3There are several approaches to find height of Binary tree using JavaScript wh 3 min read Find Distance Between Two Nodes of a Binary Tree using JavaScript In computer science, binary trees are hierarchical data structures that consist of nodes. Each node can have two children at most - one on the leÂft side and one on the right. These structures have a top-to-bottom order. Solving for distance between any two giveÂn nodes is a problem often seÂen w 7 min read Find the Subtree with the Maximum Average in a Binary Tree using JavaScript Given a binary tree, our task is to find the subtree with the maximum average in JavaScript. The average of a subtree is defined as the sum of all node values within the subtree divided by the number of nodes it contains. Example: Input: 5 / \ 6 7 / \ 3 4Output: 7ExplanationAverage of values of subt 8 min read JavaScript Program to Find Sum of Leaf Node in Binary Tree Given a binary tree, We have to find the sum of all leaf nodes. A leaf node is a node that does not have any children. Example: Input binary tree: 1 / \ 2 3 / \ / \ 4 5 6 7The sum of leaf nodes would be: 4 + 5 + 6 + 7 = 22Table of Content Using recursive approachIterative Approach Using StackUsing r 3 min read JavaScript Program to Add n Binary Strings In this article, we are going to learn about Adding n binary strings by using JavaScript. Adding n binary strings in JavaScript refers to the process of performing binary addition on a collection of n binary strings, treating them as binary numbers, and producing the sum in binary representation as 3 min read Like