# Program to find leftmost deepest node of a tree in Python

Suppose we have a binary tree; we have to find the value of the deepest node. If there are more than one deepest node, then return the leftmost deepest node.

So, if the input is like then the output will be 4 as 4 and 7 are deepest but 4 is left most.

To solve this, we will follow these steps −

• queue := a queue with one node root.

• left_max := value of root

• while size of queue > 0, do

• level_size := size of queue

• for i in range 0 to level_size, do

• temp := first element from queue and delete it

• if i is same as 0, then

• left_max := value of temp

• if left of temp is non-zero, then

• insert left of temp at the end of queue

• if right of temp is not null, then

• insert right of temp at the end of queue

• return left_max

Let us see the following implementation to get better understanding −

## Example

Live Demo

class TreeNode:
def __init__(self, value):
self.val = value
self.left = None
self.right = None

class Solution:
def solve(self, root):
queue = [root]
left_max = root.val
while len(queue) > 0:
level_size = len(queue)
for i in range(level_size):
temp = queue.pop(0)
if i == 0:
left_max = temp.val
if temp.left:
queue.append(temp.left)
if temp.right:
queue.append(temp.right)
return left_max
ob = Solution()
root = TreeNode(13)
root.left = TreeNode(12)
root.right = TreeNode(14)
root.right.left = TreeNode(16)
root.right.right = TreeNode(22)
root.right.left.left = TreeNode(4)
root.right.left.right = TreeNode(7)
print(ob.solve(root))

## Input

root = TreeNode(13)
root.left = TreeNode(12)
root.right = TreeNode(14)
root.right.left = TreeNode(16)
root.right.right = TreeNode(22)
root.right.left.left = TreeNode(4)
root.right.left.right = TreeNode(7)



## Output

4
{"ft_lang_label":"__label__en","ft_lang_prob":0.7010148,"math_prob":0.9954239,"size":3133,"snap":"2022-40-2023-06","text_gpt3_token_len":808,"char_repetition_ratio":0.17385747,"word_repetition_ratio":0.120567374,"special_character_ratio":0.26843283,"punctuation_ratio":0.110929854,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99974626,"pos_list":[0,1,2],"im_url_duplicate_count":[null,2,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-02-08T23:54:13Z\",\"WARC-Record-ID\":\"<urn:uuid:6e0aa565-1334-4f65-9c51-bcb56207e4a8>\",\"Content-Length\":\"43952\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:9cfd47f2-960a-4198-8469-c85c804b7607>\",\"WARC-Concurrent-To\":\"<urn:uuid:224612de-6d21-44f0-a973-220edce84b57>\",\"WARC-IP-Address\":\"192.229.210.176\",\"WARC-Target-URI\":\"https://www.tutorialspoint.com/program-to-find-leftmost-deepest-node-of-a-tree-in-python\",\"WARC-Payload-Digest\":\"sha1:KWWIZHQGFK6GSISSVAYV727CGZD5QNAL\",\"WARC-Block-Digest\":\"sha1:HQB2ACIF5Q7PNS6WQ6L6PTR6F6KAEG3J\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-06/CC-MAIN-2023-06_segments_1674764500983.76_warc_CC-MAIN-20230208222635-20230209012635-00509.warc.gz\"}"}