Find the Preorder Successor of a Given Node in a Binary Tree using JavaScript Last Updated : 06 May, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 tree is given by Root, Left, and Right. The preorder traversal of the above binary tree is: 1, 2, 4, 5, 3, 6, 7.Using Binary Tree TraversalThe code defines a Vertex class representing a binary tree node with key, left, right, and parent pointers. The presuccessor function determines the preorder successor of a given node, considering its children and parent relationships. It constructs a binary tree and finds the preorder successor of a selected node. Finally, it prints the result, indicating the preorder successor if found or null otherwise. The code efficiently navigates the tree, handling cases where nodes have left or right children, or no children at all. It provides a robust solution for finding preorder successors in binary trees using JavaScript. Example: This example shows the use of the above-explained approach. JavaScript class Vertex { constructor(key) { this.key = key; this.left = this.right = this.parent = null; } } function presuccessor(root, v) { if (v.left) return v.left; if (v.right) return v.right; let current = v, parent = current.parent; while (parent && parent.right === current) { current = parent; parent = parent.parent; } return parent ? parent.right : null; } // Create the binary tree let root = new Vertex(1); root.left = new Vertex(2); root.left.left = new Vertex(4); root.left.right = new Vertex(5); root.right = new Vertex(3); root.right.left = new Vertex(6); root.right.right = new Vertex(7); // Set parent pointers root.left.parent = root; root.left.left.parent = root.left; root.left.right.parent = root.left; root.right.parent = root; root.right.left.parent = root.right; root.right.right.parent = root.right; // Find the preorder successor and print the result let node = root.left.right; let successor = presuccessor(root, node); if (successor) { console.log("Preorder Successor of " + node.key + " is " + successor.key); } else { console.log("Preorder successor of " + node.key + " is null"); } OutputPreorder Successor of 5 is 3 Comment More infoAdvertise with us Next Article JavaScript Program to Delete all Nodes of a Binary Search Tree A aayushi2402 Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Questions Similar Reads Print the nodes having exactly one child in a Binary tree using JavaScript Given a binary tree, our task is to return the number of nodes in the Binary tree that have at least one child otherwise return â-1â if no such node exists. Examples: Input: 1 / \ 2 3 / \ 4 5 / 6Output: 3Explanation:There are three nodes in the Binary tree that have at least one child that are 1,2,4 4 min read JavaScript Program to Delete all Nodes of a Binary Search Tree A Binary Search Tree (BST) is a type of binary tree where each node has at most two children referred to as the left child and the right child. The value of the left child node is less than the value of the current node and the value of the right child node is greater than the value of the current n 5 min read Count the Number of Nodes in a Complete Binary tree using JavaScript We need to count the number of the nodes in the complete binary tree using JavaScript. The complete binary tree is a binary tree in which every level then except possibly the last, is completely filled and all the nodes are as far left as possible. There are several approaches for counting the numbe 4 min read Convert a Binary Tree to a Binary Search Tree using JavaScript Given a Binary Tree, the task is to convert it to a Binary Search Tree. The conversion must be done in such a way that it keeps the original structure of the Binary Tree. Example: To demonstrate the conversion of the Binary Tree to the Binary Search Tree.Input: 10 / \ 2 7 / \ 8 4Output: 8 / \ 4 10 / 2 min read Find the Distance Between Two Nodes in the Binary Tree using JavaScript Given two nodes, our task is to find the distance between two nodes in a binary tree using JavaScript, no parent pointers are given. The distance between two nodes is the minimum number of edges to be traversed to reach one node from another. Examples: Input: Binary Tree as described above in diagra 5 min read Find the maximum path sum between two leaves of a binary tree using JavaScript Given the root of a binary tree, return the maximum path sum of any non-empty path. A path in a binary tree is a sequence of nodes where each pair of adjacent nodes in the sequence has an edge connecting them. A node can only appear in the sequence at most once. Note that the path does not need to p 6 min read Like