Python program to find deepest left leaf node in a binary tree without using recursion.














































Python program to find deepest left leaf node in a binary tree without using recursion.



Description: This program is to find deepest left leaf node in a binary tree without using recursion. Algo: 1. create a class for node in a tree. 2. Form a tree by creating nodes. 3. Call a function deepest_left_node by passing root as argument. 4. Function iterates through all the left nodes, and data of that node gets stored in variable res, and also head gets updated after every iteration. 5. Loop terminates when head is None, return the value of res that which gives the value of deepest left node of the tree.

class
Node:
def __init__(self, data): self.left = None self.right = None self.data = data def deepest_left_node(self, head): while (head): res = head.data head=head.left return res root = Node(1) root.left = Node(2) root.right = Node(3) root.left.left = Node(4) root.left.right = Node(5) root.left.left.left= Node(10) print("deepest left node of this binary tree is:",root.deepest_left_node(root))

Output:
#for the given tree output is:
deepest left node of this binary tree is: 10

Comments