dimanche 30 mai 2010

Depth of binary search tree without using recursion

Write a program to find depth of binary search tree without using recursion

def depth(tree):
   max_depth = 0
   todo = [[tree,0]]
   while len(todo) > 0:
      node,depth = todo.pop(0)
      if not node.left and not node.right:
         if depth > max_depth:
           max_depth = depth
      if node.left:
         todo.push([node.left,depth+1])         
      if node.right:
         todo.push([node.right,depth+1])         

Aucun commentaire:

Enregistrer un commentaire