# 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.

Examples:

```Input :
1
/
2     3
/      /
4      5    6

7     8
/
9         10

Output : 9
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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,

## C++

 `// CPP program to find deepest left leaf ` `// node of binary tree ` `#include ` `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 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 "````; ` `    ``return` `0; ` `} `

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

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

Output:

```Deepest Left Leaf Node :: 9
```

Mandeep Singh

## tags:

Tree cpp-queue Tree
{"ft_lang_label":"__label__en","ft_lang_prob":0.56964916,"math_prob":0.9633404,"size":3529,"snap":"2020-24-2020-29","text_gpt3_token_len":1008,"char_repetition_ratio":0.17191489,"word_repetition_ratio":0.035971224,"special_character_ratio":0.30716917,"punctuation_ratio":0.17613636,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99461573,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-05-28T16:01:26Z\",\"WARC-Record-ID\":\"<urn:uuid:1eae9243-2d7b-4edd-b06e-8995aae91182>\",\"Content-Length\":\"33544\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:9b885fa0-927f-4e8f-8ed5-c7af4db76719>\",\"WARC-Concurrent-To\":\"<urn:uuid:4db5f00b-9348-4cbf-bac1-ca5db0f19571>\",\"WARC-IP-Address\":\"104.27.154.206\",\"WARC-Target-URI\":\"https://tutorialspoint.dev/data-structure/binary-tree-data-structure/deepest-left-leaf-node-binary-tree-iterative-approach\",\"WARC-Payload-Digest\":\"sha1:FMA2A55KGMOCYDST334TD5LJHGNU3DZC\",\"WARC-Block-Digest\":\"sha1:VYMYIMIXUQSV5OAO7M4LJCBZA4MBDGCG\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-24/CC-MAIN-2020-24_segments_1590347399820.9_warc_CC-MAIN-20200528135528-20200528165528-00160.warc.gz\"}"}