Sollicitatievraag bij Google

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

Antwoorden op sollicitatievragen

Anoniem

20 aug 2010

Python implementation using a stack. def depth(root): st = [(root, 0)] maxd = 0 while len(st): node, dpt = st.pop() maxd = max(maxd, dpt) if node['left']: st.append((node['left'], dpt + 1)) if node['right']: st.append((node['right'], dpt + 1)) return maxd

1

Anoniem

17 mei 2010

Q.enqueue rootNode,1 depth=0 while q,count>0 [n,d] = q.dequeue depth = max(d,depth) if n.left q.enqueue(n.left,d+1) if n.right q.enqueue(n.right, d+1) end return depth