Return the Leftmost Value in the Last Row of the Binary Tree using JavaScript
Last Updated :
16 May, 2024
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 binary tree is 7.
Binary TreeBelow are the approaches to return the leftmost value in the last row of the binary tree using JavaScript:
Recursive Approach
The code defines a Node class to represent nodes in a binary tree and initializes variables for tracking the maximum level, root node, and result. It utilizes a recursive function 'deepestLeftLeafUtil' to traverse the tree, identifying the leftmost leaf node in the last row based on the depth. A binary tree is constructed, and the deepestLeftLeaf function is called to find the leftmost leaf node. If found, its data value is printed otherwise a message indicating no left leaf in the tree is printed.
Example: The example below shows how to return the leftmost value in the last row of the binary tree using the Recursive Approach.
JavaScript
class Node {
constructor(data) {
this.left = null;
this.right = null;
this.data = data;
}
}
let maxlevel = 0;
let root;
let result;
function deepestLeftLeafUtil(node, lvl, isLeft) {
// Base case
if (node == null)
return;
if (isLeft != false &&
node.left == null &&
node.right == null &&
lvl > maxlevel) {
result = node;
maxlevel = lvl;
}
deepestLeftLeafUtil(node.left, lvl + 1, true);
deepestLeftLeafUtil(node.right, lvl + 1, false);
}
// A wrapper over deepestLeftLeafUtil()
function deepestLeftLeaf(node) {
deepestLeftLeafUtil(node, 0, false);
}
// Driver code
root = new Node(1);
root.left = new Node(2);
root.right = new Node(3);
root.left.left = new Node(4);
root.right.left = new Node(5);
root.right.right = new Node(6);
root.right.left.left = new Node(7);
deepestLeftLeaf(root);
if (result != null)
console.log(result.data);
else
console.log("There is no left leaf in the given tree");
Time Complexity: O(n), where n is the number of nodes in a given binary tree.
Space Complexity: O(n).
Iterative Approach(Using Level Order Traversal)
In this approach, a 'Node' class represents nodes in a binary tree and provides a utility function 'newNode' to create new nodes. The 'deepestLeftLeaf' function iterates through the tree level by level using a queue, tracking the leftmost node encountered at each level. It returns the leftmost leaf node found in the last row. A binary tree is constructed, and the "deepestLeftLeaf" function is called to find the leftmost leaf node in the last row. If found, its data value is printed otherwise a message indicates that there is no left leaf in the tree.
Example: The example below shows return the leftmost value in the last row of the binary tree in JavaScript using Iterative Approach.
JavaScript
class Node {
constructor(data) {
this.data = data;
this.left = null;
this.right = null;
}
}
function newNode(data) {
return new Node(data);
}
function deepestLeftLeaf(root) {
if (root == null) return null;
let ans = null;
let q = [];
q.push(root);
while (q.length > 0) {
let front_node = q.shift();
if (front_node.left) {
q.push(front_node.left);
ans = front_node.left;
}
if (front_node.right) q.push(front_node.right);
}
return ans;
}
let root = new Node(1);
root.left = new Node(2);
root.right = new Node(3);
root.left.left = new Node(4);
root.right.left = new Node(5);
root.right.right = new Node(6);
root.right.left.left = new Node(7);
let result = deepestLeftLeaf(root);
if (result)
console.log(result.data);
else
console.log("There is no left leaf in the given tree.");
Time Complexity: O(n), where n is the number of nodes in given binary tree.
Space Complexity: O(n) .
Similar Reads
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
Find the Mode in a Binary Search Tree using JavaScript One can find the mode in a binary search tree using JavaScript. The mode in the binary search tree will be the value of the node that occurs with the highest frequency among all the nodes in the tree. If there is more than one mode present in the binary search tree then return all modes present in t
3 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
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
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
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
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
JavaScript Program for Postorder Traversal of Binary Tree In a binary tree, a node always has its two children - left and right children. The left and right children of any node can be another node or may be NULL. The postorder traversal of a binary tree is traversing the root in the following manner - first the left child node, then the right child node,
3 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
JavaScript Program to Find the Position of the Rightmost Set Bit Given a number, the task is to find the position of the rightmost set bit. The indexing is from right to left, where the rightmost bit position starts from 1. Example: Input: n = 20Output: 3Explanation: Binary Representation of 20 is 10100, hence position of first set bit from right is 3.Input: n =
3 min read