Sunday, June 1st, 2008
The last problem in the Trees chapter of Programming Interviews Exposed was about finding the lowest common ancestor between two nodes of a binary tree. Starting from the head, if you find that the nodes that you're looking for straddle the node you're on, then you've found your lowest common ...
Posted in programming | 1 Comment »
Saturday, May 31st, 2008
Another question posed in the Programming Interviews Exposed book. A pre-ordered traversal of a binary tree (counterclockwise starting at root, printing nodes as you encounter them) is pretty straight forward, and a very natural thing to implement with recursion:
>>> class Node:
def __init__(self,value,left=None,right=None):
self.value=value;self.left=left;self.right=right
>>> node1=Node(1)
>>> node2=Node(2)
>>> node3=Node(3,node1,node2)
>>> node4=Node(4)
>>> node5=Node(5,node3,node4)
>>> def traverse(node):
if ...
Posted in programming | 4 Comments »