Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # http://code2begin.blogspot.com
- # Program to print leaf nodes of binary tree
- # node class
- class node:
- def __init__(self, element):
- self.data = element
- self.left = None
- self.right = None
- # function to print the leaf nodes of the binary tree
- def print_leaves(root):
- if root is not None:
- print_leaves(root.left)
- if root.left is None and root.right is None:
- print(root.data, end=" ")
- print_leaves(root.right)
- head = node(1)
- head.left = node(2)
- head.right = node(3)
- head.left.left = node(4)
- head.left.right = node(5)
- head.right.right = node(6)
- head.left.left.right = node(7)
- head.right.right.left = node(8)
- head.left.left.right.left = node(9)
- head.left.left.right.left.left = node(10)
- head.right.right.left.right = node(11)
- print("leaf nodes of the given tree are : ")
- print_leaves(head)
Add Comment
Please, Sign In to add comment