Kth Ancestor in Binary Tree using JavaScript Last Updated : 16 May, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 each node, the number of edges from the node to the tree's root and store it along with each node's parent.Using the stored information, trace back from the given node to its k-th ancestor by using the depth and parent pointers.Traverse the binary tree to record each node's parent and depth. This can be done using a simple BFS.For the given node, use the depth information to jump back k levels to find and return the k-th ancestor.Example: The example below shows kth ancestor in Binary tree using JavaScript. JavaScript class TreeNode { constructor(value = 0, left = null, right = null) { this.value = value; this.left = left; this.right = right; } } function kthAncestor(root, node, k) { let parentMap = new Map(); let depthMap = new Map(); function buildMaps(node, parent = null, depth = 0) { if (node !== null) { parentMap.set(node, parent); depthMap.set(node, depth); buildMaps(node.left, node, depth + 1); buildMaps(node.right, node, depth + 1); } } buildMaps(root); let ancestor = node; while (k > 0 && ancestor !== null) { ancestor = parentMap.get(ancestor); k--; } return ancestor ? ancestor.value : null; } let root = new TreeNode(1, new TreeNode(2, new TreeNode(4), new TreeNode(5)), new TreeNode(3, null, new TreeNode(6)) ); let node = root.left.right; console.log(kthAncestor(root, node, 2)); Output1 Time Complexity: O(n) time, where n is the number of nodes in the tree. Space Complexity: O(n). Comment More infoAdvertise with us Next Article Insert a Node in Binary tree using JavaScript S sachinparmar98134 Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Program Similar Reads LCA in Binary Tree using JavaScript The lowest common ancestor between two nodes n1 and n2 in a binary tree is defined as the lowest node in a Tree that has both n1 and n2 as descendants. If either n1 or n2 is not present in the tree, or if they are not connected through a common ancestor, the LCA of the binary tree is NULL. Below is 4 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 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 Count all K-Sum Paths in a Binary Tree using JavaScript 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 representi 2 min read Search a Given Key in BST using JavaScript Given a binary search tree and a key, determine if the key is present in the tree. If the key is present, return the node containing the key otherwise, return null. Example: Input : {15,20,10,8,12,16,25}, Key : 20Output : 20 found 15 / \ 10 20 / \ / \ 8 12 16 25Table of Content Recursive ApproachIte 3 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 Like