VOOZH about

URL: https://www.geeksforgeeks.org/dsa/deepest-left-leaf-node-in-a-binary-tree/

⇱ Deepest left leaf node in a binary tree - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Deepest left leaf node in a binary tree

Last Updated : 7 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. 

 1
 / \
 2 3
 / / \ 
 4 5 6
 \ \
 7 8
 / \
 9 10 

The idea is to recursively traverse the given binary tree and while traversing, maintain “level” which will store the current node’s level in the tree. If current node is left leaf, then check if its level is more than the level of deepest left leaf seen so far. If level is more then update the result. If current node is not leaf, then recursively find maximum depth in left and right subtrees, and return maximum of the two depths.

Implementation:


Output
The deepest left child is 9

Time Complexity: The function does a simple traversal of the tree, so the complexity is O(n). 

Auxiliary Space: O(n) for call stack because using recursion

Iterative Approach(Using Level Order Traversal):
Follow the below steps to solve the above problem:
1) We simply traverse the tree using Level Order Traversal with queue data structure.
2) If current node has left child then we update our answer with left child.
3) Finally return the ans node.

Below is the implementation of above approach:


Output
The deepest left child is 9

Time Complexity: O(N) where N is the number of nodes in given binary tree.
Auxiliary Space: O(N) due to queue data structure.

Comment
Article Tags:
Article Tags: