Insert a Node in Binary tree using JavaScript Last Updated : 24 Apr, 2024 Comments Improve Suggest changes Like Article Like Report 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. There are various approaches to inserting a node in a binary tree using JavaScript which are as follows: Table of Content Using Recursive ApproachUsing Iterative ApproachUsing Recursive ApproachIn the recursive approach, we define a recursive function to insert a node into the binary tree. If the current node is null, we create a new node and return it. Otherwise, we recursively traverse the tree to the left or right based on the value of the new node. Example: This example uses a recursive approach to Insert a Node in a Binary tree. JavaScript class Node { constructor(value) { this.value = value; this.left = null; this.right = null; } } class BinaryTree { constructor() { this.root = null; } insertRecursively(value, node = this.root) { if (!node) { return new Node(value); } if (value < node.value) { node.left = this .insertRecursively(value, node.left); } else if (value > node.value) { node.right = this .insertRecursively(value, node.right); } return node; } } // Create a binary tree const binaryTree = new BinaryTree(); binaryTree.root = binaryTree. insertRecursively(5); binaryTree.insertRecursively(3); binaryTree.insertRecursively(8); binaryTree.insertRecursively(1); binaryTree.insertRecursively(4); binaryTree.insertRecursively(6); console.log(binaryTree.root.right.left.value); Output6 Time complexity: O(h), where h is the height of the binary tree. Space complexity: O(n) Using Iterative ApproachIterative approach uses a loop to traverse the binary tree iteratively. It starts at the root node and compares the value of the new node with the current node. If the new node's value is less than the current node's value, it moves to the left child; otherwise, it moves to the right child. This process continues until it reaches a leaf node, where it inserts the new node. Example: This example uses Iterative approach to Insert a Node in Binary tree. JavaScript class Node { constructor(value) { this.value = value; this.left = null; this.right = null; } } class BinaryTree { constructor() { this.root = null; } insertIteratively(value) { const newNode = new Node(value); if (!this.root) { this.root = newNode; return; } let current = this.root; while (true) { if (value < current.value) { if (!current.left) { current.left = newNode; return; } current = current.left; } else { if (!current.right) { current .right = newNode; return; } current = current.right; } } } } // Create a binary tree const binaryTree = new BinaryTree(); binaryTree.insertIteratively(5); binaryTree.insertIteratively(3); binaryTree.insertIteratively(8); binaryTree.insertIteratively(1); binaryTree.insertIteratively(4); binaryTree.insertIteratively(6); console.log(binaryTree.root.right.left.value); Output6 Time complexity: O(h), where h is the height of the binary tree. Space complexity: O(1) Comment More infoAdvertise with us Next Article Insert a Node in Binary tree using JavaScript yuvrajghule281 Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Program Similar Reads 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 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 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 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 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 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 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 JavaScript Program to Print Root to Node Path in a Binary Tree 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 C 3 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 Like