r********t 发帖数: 395 | 1 http://careercup.com/question?id=405592
Q: Take a tree (binary or otherwise), write a method in any language that,
when given the root node, will print out the tree in level order. With a new
line after the end of every level.
Helper methods are ok, big O run time efficiency doesn't matter (though
obviously a quicker solution is better). Do not destroy original tree.
A:
1. queue.push(root)
2. queue.push(marker)
3. while queue not empty
node = queue.pop()
if(node == marker)
| h**k 发帖数: 3368 | 2 这里marker就是一个标志,你可以用null。 |
|