JavaScript Program to Print Root to Node Path in a Binary Tree Last Updated : 10 May, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report Given a binary tree and a target Node value, we have to find the path from the root of the tree to the node containing the given value. Example: Input: binary tree 1 / \ 2 3 / \4 5Target node: 4Output:path: 1->2->4Below are the approaches to print Root to Node Path in a Binary Tree: Table of Content Recursive approachIterative approachRecursive approachIn this approach we define a recursive function to find the path from the root to a target node in the binary tree. first function checks if the root is null, if it is then function returns null indicating that the target node was not found in this subtree. Then function recursively explores the left and right subtrees and pushes the current node's value to the path array. When the target node is found it prints the path. If the target node is not found in either subtree, the function backtracks by removing the last element from the path array. Example: To demonstrate printing Root to Node Path in a Binary Tree using recursive approach. JavaScript class Node { constructor(val) { this.val = val; this.left = null; this.right = null; } } function printPath(root, target, path = []) { if (root === null) return null; path.push(root.val); if (root.val === target) { console.log(path.join(' -> ')); return path; } if ( (root.left !== null && printPath(root.left, target, path)) || (root.right !== null && printPath(root.right, target, path)) ) { return path; } path.pop(); return null; } const root = new Node(1); root.left = new Node(2); root.right = new Node(3); root.left.left = new Node(4); root.left.right = new Node(5); root.right.right = new Node(7); root.right.right.left = new Node(8); const value = 7; const path = printPath(root, value); Output1 -> 3 -> 7 Time Complexity: O(n) Space Complexity: O(n) Iterative approachThe iterative approach uses a stack to perform a depth-first search iteratively. We initialize a stack to keep track of nodes to visit. we use a while loop explore the tree iteratively. In each iteration we pop a node from the stack and its value is checked against the target value. If the current node's value matches the target value then we print the path. If the target node is not found then we push child nodes to the stack along with an updated path array that includes the child node's value. This process continues until the stack is empty until stack becomes empty to ensure we visited every node. Example: To demonstrate printing root to node path in a binary tree using iterative approach. JavaScript class TreeNode { constructor(value) { this.value = value; this.left = null; this.right = null; } } function printPath(root, target) { if (!root) return null; const stack = [{ node: root, path: [root.value] }]; while (stack.length > 0) { const { node, path } = stack.pop(); if (node.value === target) { console.log(path.join(" -> ")); return; } if (node.right) { stack .push({ node: node.right, path: path.concat(node.right.value) }); } if (node.left) { stack .push({ node: node.left, path: path.concat(node.left.value) }); } } } const root = new TreeNode(1); root.left = new TreeNode(2); root.right = new TreeNode(3); root.left.left = new TreeNode(4); root.left.right = new TreeNode(5); root.right.left = new TreeNode(6); root.right.right = new TreeNode(7); const targetNode = 6; printPath(root, targetNode); Output1 -> 3 -> 6 Time Complexity: O(n) Space Complexity: O(n) Comment More infoAdvertise with us Next Article JavaScript Program for Rightmost and Leftmost Node of a Binary Tree G ghuleyogesh Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Program Similar Reads 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 Return the Leftmost Value in the Last Row of the Binary Tree using JavaScript Given the root of a binary tree, our task is to return the leftmost value in the last row of the binary tree in JavaScript. Here we use recursive and iterative approaches to return the leftmost value in the last row. Explanation: The image below illustrates the leftmost value in the last row of the 3 min read 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 JavaScript Program for Rightmost and Leftmost Node of a Binary Tree A binary tree is a fundamental data structure consisting of nodes, each with at most two children: a left child and a right child. The topmost node is called the root. Binary trees support operations such as insertion, deletion, and various traversal methods like in-order, pre-order, and post-order 3 min read Java Program for the Preorder Tree Traversal in Binary Tree Preorder traversal is the method used to traverse the tree data structure. In the preorder traversal, the nodes are visited in the order: of root, left subtree, and right subtree. It is called as "preorder" because the root node is visited before its children. This traversal technique is widely used 2 min read Find the Preorder Successor of a Given Node in a Binary Tree using JavaScript The Preorder successor of a given node is a node that occurs after the given node in the preorder traversal of the binary tree. The preorder traversal is a traversal in which the root node is visited first, then the left child, and then the right child. Example: The pre-order successor of a binary t 2 min read Like