Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Python program to for tree traversals
- # A class that represents an individual node in a
- # Binary Tree
- class Node:
- def __init__(self,key):
- self.left = None
- self.right = None
- self.val = key
- # A function to do preorder tree traversal
- def printPreorder(root):
- if root:
- # First print the data of node
- print(root.val),
- # Then recur on left child
- printPreorder(root.left)
- # Finally recur on right child
- printPreorder(root.right)
- # Driver code
- root = Node(1)
- root.left = Node(2)
- root.right = Node(3)
- root.left.left = Node(4)
- root.left.right = Node(5)
- print "Preorder traversal of binary tree is"
- printPreorder(root)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement