Deepest left leaf node in a binary tree | iterative approach

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.


Input : 
     /   \
    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,






// 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;
    Node* result = NULL;
    // traverse until the queue is empty
    while (!q.empty()) {
        Node* temp = q.front();
        // Since we go level by level, the last 
        // stored left leaf node is deepest one,
        if (temp->left) {
            if (!temp->left->left && !temp->left->right)
                result = temp->left;
        if (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;
        cout << "No result, left leaf not found\n";
    return 0;



# 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 poers.
class newnode:

# Constructor to create a new node
def __init__(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 = []

result = None

# traverse until the queue is empty
while (len(q)):
temp = q[0]

if (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):

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 ::”,
print(“No result, Left leaf not found”)

# This code is contributed by
# Shubham Singh(SHUBHAMSINGH10)


Deepest Left Leaf Node :: 9

Mandeep Singh

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : SHUBHAMSINGH10

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at to report any issue with the above content.