Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.75 KB | None | 0 0
  1. # Python program to for tree traversals
  2.  
  3. # A class that represents an individual node in a
  4. # Binary Tree
  5. class Node:
  6.     def __init__(self,key):
  7.         self.left = None
  8.         self.right = None
  9.         self.val = key
  10.  
  11. # A function to do preorder tree traversal
  12. def printPreorder(root):
  13.  
  14.     if root:
  15.  
  16.         # First print the data of node
  17.         print(root.val),
  18.  
  19.         # Then recur on left child
  20.         printPreorder(root.left)
  21.  
  22.         # Finally recur on right child
  23.         printPreorder(root.right)
  24.  
  25. # Driver code
  26. root = Node(1)
  27. root.left      = Node(2)
  28. root.right     = Node(3)
  29. root.left.left  = Node(4)
  30. root.left.right  = Node(5)
  31. print "Preorder traversal of binary tree is"
  32. printPreorder(root)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement