Deepest left leaf node in a binary tree | iterative approach
Last Updated :
17 Mar, 2023
Given a Binary Tree, find the deepest leaf node that is left child of its parent. For example, consider the following tree. The deepest left leaf node is the node with value 9.
Examples:
Input :
1
/ \
2 3
/ / \
4 5 6
\ \
7 8
/ \
9 10
Output : 9
Recursive approach to this problem is discussed here
For iterative approach, idea is similar to Method 2 of level order traversal
The idea is to traverse the tree iteratively and whenever a left tree node is pushed to queue, check if it is leaf node, if it's leaf node, then update the result. Since we go level by level, the last stored leaf node is deepest one,
Algorithm:
- Define a Node structure that has data, left, and right pointers.
- Create a function newNode that takes data as an argument and returns a new node with data set to data, and left and right pointers set to NULL.
- Create a function getDeepestLeftLeafNode that takes the root of a binary tree as an argument and returns the deepest left leaf node of the tree.
- Initialize a queue q and add the root node of the binary tree to the queue.
- Initialize a pointer result to NULL.
- Use a while loop to traverse the binary tree level by level until the queue becomes empty.
- Within the while loop, pop the front element of the queue and store it in a temporary pointer temp.
- If temp has a left child, add it to the queue, and check if it is a leaf node (i.e., it does not have left or right child).
- If the left child of temp is a leaf node, set result to temp->left.
- If temp has a right child, add it to the queue.
- Return result which will contain the deepest left leaf node of the binary tree.
- In the main function, construct a binary tree and call getDeepestLeftLeafNode to find the deepest left leaf node of the tree.
- If result is not NULL, print the data of the deepest left leaf node, otherwise print "No result, left leaf not found".
Implementation:
C++
// CPP program to find deepest left leaf
// node of binary tree
#include <bits/stdc++.h>
using namespace std;
// tree node
struct Node {
int data;
Node *left, *right;
};
// returns a new tree Node
Node* newNode(int data)
{
Node* temp = new Node();
temp->data = data;
temp->left = temp->right = NULL;
return temp;
}
// return the deepest left leaf node
// of binary tree
Node* getDeepestLeftLeafNode(Node* root)
{
if (!root)
return NULL;
// create a queue for level order traversal
queue<Node*> q;
q.push(root);
Node* result = NULL;
// traverse until the queue is empty
while (!q.empty()) {
Node* temp = q.front();
q.pop();
// Since we go level by level, the last
// stored left leaf node is deepest one,
if (temp->left) {
q.push(temp->left);
if (!temp->left->left && !temp->left->right)
result = temp->left;
}
if (temp->right)
q.push(temp->right);
}
return result;
}
// driver program
int main()
{
// construct a tree
Node* root = newNode(1);
root->left = newNode(2);
root->right = newNode(3);
root->left->left = newNode(4);
root->right->left = newNode(5);
root->right->right = newNode(6);
root->right->left->right = newNode(7);
root->right->right->right = newNode(8);
root->right->left->right->left = newNode(9);
root->right->right->right->right = newNode(10);
Node* result = getDeepestLeftLeafNode(root);
if (result)
cout << "Deepest Left Leaf Node :: "
<< result->data << endl;
else
cout << "No result, left leaf not found\n";
return 0;
}
Java
// Java program to find deepest left leaf
// node of binary tree
import java.util.*;
public class GFG
{
// tree node
static class Node
{
int data;
Node left, right;
};
// returns a new tree Node
static Node newNode(int data)
{
Node temp = new Node();
temp.data = data;
temp.left = temp.right = null;
return temp;
}
// return the deepest left leaf node
// of binary tree
static Node getDeepestLeftLeafNode(Node root)
{
if (root == null)
return null;
// create a queue for level order traversal
Queue<Node> q = new LinkedList<>();
q.add(root);
Node result = null;
// traverse until the queue is empty
while (!q.isEmpty())
{
Node temp = q.peek();
q.remove();
// Since we go level by level, the last
// stored left leaf node is deepest one,
if (temp.left != null)
{
q.add(temp.left);
if (temp.left.left == null &&
temp.left.right == null)
result = temp.left;
}
if (temp.right != null)
q.add(temp.right);
}
return result;
}
// Driver Code
public static void main(String[] args)
{
// construct a tree
Node root = newNode(1);
root.left = newNode(2);
root.right = newNode(3);
root.left.left = newNode(4);
root.right.left = newNode(5);
root.right.right = newNode(6);
root.right.left.right = newNode(7);
root.right.right.right = newNode(8);
root.right.left.right.left = newNode(9);
root.right.right.right.right = newNode(10);
Node result = getDeepestLeftLeafNode(root);
if (result != null)
System.out.println("Deepest Left Leaf Node :: " +
result.data);
else
System.out.println("No result, " +
"left leaf not found");
}
}
// This code is contributed by Rajput-Ji
Python3
# Python3 program to find deepest
# left leaf Binary search Tree
_MIN = -2147483648
_MAX = 2147483648
# Helper function that allocates a new
# node with the given data and None
# left and right pointers.
class newnode:
# Constructor to create a new node
def __init__(self, data):
self.data = data
self.left = None
self.right = None
# utility function to return deepest
# left leaf node
def getDeepestLeftLeafNode(root) :
if (not root):
return None
# create a queue for level
# order traversal
q = []
q.append(root)
result = None
# traverse until the queue is empty
while (len(q)):
temp = q[0]
q.pop(0)
if (temp.left):
q.append(temp.left)
if (not temp.left.left and
not temp.left.right):
result = temp.left
# Since we go level by level,
# the last stored right leaf
# node is deepest one
if (temp.right):
q.append(temp.right)
return result
# Driver Code
if __name__ == '__main__':
# create a binary tree
root = newnode(1)
root.left = newnode(2)
root.right = newnode(3)
root.left.Left = newnode(4)
root.right.left = newnode(5)
root.right.right = newnode(6)
root.right.left.right = newnode(7)
root.right.right.right = newnode(8)
root.right.left.right.left = newnode(9)
root.right.right.right.right = newnode(10)
result = getDeepestLeftLeafNode(root)
if result:
print("Deepest Left Leaf Node ::",
result.data)
else:
print("No result, Left leaf not found")
# This code is contributed by
# Shubham Singh(SHUBHAMSINGH10)
C#
// C# program to find deepest left leaf
// node of binary tree
using System;
using System.Collections.Generic;
class GFG
{
// tree node
class Node
{
public int data;
public Node left, right;
};
// returns a new tree Node
static Node newNode(int data)
{
Node temp = new Node();
temp.data = data;
temp.left = temp.right = null;
return temp;
}
// return the deepest left leaf node
// of binary tree
static Node getDeepestLeftLeafNode(Node root)
{
if (root == null)
return null;
// create a queue for level order traversal
Queue<Node> q = new Queue<Node>();
q.Enqueue(root);
Node result = null;
// traverse until the queue is empty
while (q.Count != 0)
{
Node temp = q.Peek();
q.Dequeue();
// Since we go level by level, the last
// stored left leaf node is deepest one,
if (temp.left != null)
{
q.Enqueue(temp.left);
if (temp.left.left == null &&
temp.left.right == null)
result = temp.left;
}
if (temp.right != null)
q.Enqueue(temp.right);
}
return result;
}
// Driver Code
public static void Main(String[] args)
{
// construct a tree
Node root = newNode(1);
root.left = newNode(2);
root.right = newNode(3);
root.left.left = newNode(4);
root.right.left = newNode(5);
root.right.right = newNode(6);
root.right.left.right = newNode(7);
root.right.right.right = newNode(8);
root.right.left.right.left = newNode(9);
root.right.right.right.right = newNode(10);
Node result = getDeepestLeftLeafNode(root);
if (result != null)
Console.WriteLine("Deepest Left Leaf Node :: " +
result.data);
else
Console.WriteLine("No result, " +
"left leaf not found");
}
}
// This code is contributed by Rajput-Ji
JavaScript
<script>
// JavaScript program to find deepest
// left leaf node of binary tree
class Node
{
constructor(data) {
this.left = null;
this.right = null;
this.data = data;
}
}
// returns a new tree Node
function newNode(data)
{
let temp = new Node(data);
return temp;
}
// return the deepest left leaf node
// of binary tree
function getDeepestLeftLeafNode(root)
{
if (root == null)
return null;
// create a queue for level order traversal
let q = [];
q.push(root);
let result = null;
// traverse until the queue is empty
while (q.length > 0)
{
let temp = q[0];
q.shift();
// Since we go level by level, the last
// stored left leaf node is deepest one,
if (temp.left != null)
{
q.push(temp.left);
if (temp.left.left == null &&
temp.left.right == null)
result = temp.left;
}
if (temp.right != null)
q.push(temp.right);
}
return result;
}
// construct a tree
let root = newNode(1);
root.left = newNode(2);
root.right = newNode(3);
root.left.left = newNode(4);
root.right.left = newNode(5);
root.right.right = newNode(6);
root.right.left.right = newNode(7);
root.right.right.right = newNode(8);
root.right.left.right.left = newNode(9);
root.right.right.right.right = newNode(10);
let result = getDeepestLeftLeafNode(root);
if (result != null)
document.write("Deepest Left Leaf Node :: " +
result.data);
else
document.write("No result, " +
"left leaf not found");
</script>
OutputDeepest Left Leaf Node :: 9
Time complexity: O(n) where n is no of nodes in a given binary tree
Auxiliary space: O(n)
Similar Reads
Deepest right leaf node in a binary tree | Iterative approach Given a Binary Tree, find the deepest leaf node that is the right child of its parent. For example, consider the following tree. The deepest right leaf node is the node with the value 10. Examples: Input : 1 / \ 2 3 \ / \ 4 5 6 \ \ 7 8 / \ 9 10 Output : 10 The idea is similar to Method 2 of level or
7 min read
Get level of a node in binary tree | iterative approach Given a Binary Tree and a key, write a function that returns level of the key. For example, consider the following tree. If the input key is 3, then your function should return 1. If the input key is 4, then your function should return 3. And for key which is not present in key, then your function s
10 min read
Deepest left leaf node in a binary tree Given a Binary Tree, find the deepest leaf node that is left child of its parent. For example, consider the following tree. The deepest left leaf node is the node with value 9. 1 / \ 2 3 / / \ 4 5 6 \ \ 7 8 / \ 9 10 The idea is to recursively traverse the given binary tree and while traversing, main
13 min read
Iterative approach to check if a Binary Tree is Perfect Given a Binary Tree, the task is to check whether the given Binary Tree is a perfect Binary Tree or not.A Binary tree is a Perfect Binary Tree in which all internal nodes have two children and all leaves are at the same level.Examples: Input : 1 / \ 2 3 / \ / \ 4 5 6 7 Output : Yes Input : 20 / \ 8
9 min read
Sum of nodes at maximum depth of a Binary Tree | Iterative Approach Given a root node to a tree, find the sum of all the leaf nodes which are at maximum depth from the root node.Example: 1 / \ 2 3 / \ / \ 4 5 6 7Input : root(of above tree)Output : 22Explanation:Nodes at maximum depth are 4, 5, 6, 7. So, the sum of these nodes = 22Approach: There exists a recursive a
7 min read
Print All Leaf Nodes of a Binary Tree from left to right | Set-2 ( Iterative Approach ) Given a Binary Tree, the task is to print the leaf nodes from left to right. The nodes must be printed in the order they appear from left to right.Examples: Input : 1 / \ 2 3 / \ / \ 4 5 6 7 Output :4 5 6 7 Input : 4 / \ 5 9 / \ / \ 8 3 7 2 / / \ 12 6 1 Output :12 3 7 6 1 We have already discussed t
8 min read
Find the Deepest Node in a Binary Tree Given a binary tree, find the deepÂest node in it. Examples: Input : Root of below tree 1 / \ 2 3 / \ / \ 4 5 6 7 \ 8 Output : 8 Input : Root of below tree 1 / \ 2 3 / 6 Output : 6 Method 1: The idea is to do Inorder traversal of a given binary tree. While doing Inorder traversal, we pass level of c
15 min read
Iterative method to find ancestors of a given binary tree Given a binary tree, print all the ancestors of a particular key existing in the tree without using recursion.Here we will be discussing the implementation for the above problem. Examples: Input : 1 / \ 2 7 / \ / \ 3 5 8 9 / \ / 4 6 10 Key = 6 Output : 5 2 1 Ancestors of 6 are 5, 2 and 1. The idea i
12 min read
Closest leaf to a given node in Binary Tree Given a Binary Tree and a node x in it, find distance of the closest leaf to x in Binary Tree. If given node itself is a leaf, then distance is 0.Examples: Input: Root of below tree And x = pointer to node 13 10 / \ 12 13 / 14 Output 1 Distance 1. Closest leaf is 14. Input: Root of below tree And x
12 min read
Lowest Common Ancestor of the deepest leaves of a Binary Tree Given a Binary Tree consisting of N nodes having distinct values from the range [1, N], the task is to find the lowest common ancestor of the deepest leaves of the binary tree. Examples: Input: Output: 1Explanation: The deepest leaf nodes of the tree are {8, 9, 10}. Lowest common ancestor of these n
10 min read