Mercurial > louis > peeves
view graphs_trees/next_smallest_integer_in_bst/solution.py @ 0:20fea762903e
Import some exercises and solutions
author | Louis Opter <kalessin@kalessin.fr> |
---|---|
date | Sat, 29 Jun 2013 19:30:31 -0700 |
parents | |
children |
line wrap: on
line source
# NOTE: just a dump from the phone screen. class Node: def __init__(self, val): self.right = None self.left = None self.val = val # Not perfect, if the n is not in tree then the biggest element of the tree is # not returned. # # Or if the tree is just a root node, then it doesn't work (that edge case # could be solved with a separate function for the recursion). # # (Also actually untested, but looks like it works) def next_smallest(root, n): if root is None: return rv = next_smallest(root.left, n) if rv is not None: return rv if root.right == n: return root.val rv = next_smallest(root.right, n) if rv is not None: return rv return None