Deepest right leaf node in a binary tree | Iterative approach

Given a Binary Tree, find the deepest leaf node that is right child of its parent. For example, consider the following tree. The deepest right leaf node is the node with 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 order traversal

Traverse the tree level by level and while pushing right child to queue, check if it is leaf node, if it’s leaf node, then update the result and since we are traversing level by level, the last stored right leaf will be the deepest right leaf node.

// CPP program to find deepest right 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 right leaf node
// of binary tree
Node* getDeepestRightLeafNode(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();
 
        
        if (temp->left) {
            q.push(temp->left);
        }
        
        // Since we go level by level, the last 
        // stored right leaf node is deepest one 
        if (temp->right){
            q.push(temp->right);
            if (!temp->right->left && !temp->right->right)
                result = 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->right = 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 = getDeepestRightLeafNode(root);
    if (result)
        cout << "Deepest Right Leaf Node :: "
             << result->data << endl;
    else
        cout << "No result, right leaf not found\n";
    return 0;
}

Output:

Deepest Right Leaf Node :: 10

Time Complexity : O(n)

Mandeep Singh





Check out this Author's contributed articles.

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

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.

Recommended Posts:



2 Average Difficulty : 2/5.0
Based on 1 vote(s)