层次遍历又称广度优先遍历,使用队列的数据结构
def depth(root):
if root is None:
return 0
else:
return (1 + max(depth(root.right),depth(root.left)))
print depth(root)
def level(root):
if root is None:
return 0
else:
q = []
q.append(root)
while q :
current = q.pop(0)
print (current.data)
if current.left is not None:
q.append(current.left)
if current.right is not None:
q.append(current.right)
level(root)