Breadth-first
Also, listed below is pseudocode for a simple queue
based level order traversal, and will require space proportional to the
maximum number of nodes at a given depth. This can be as much as the
total number of nodes / 2. A more space-efficient approach for this type
of traversal can be implemented using an iterative deepening depth-first search.
levelorder(root)
q = empty queue
q.enqueue(root)
while not q.empty do
node := q.dequeue()
visit(node)
if node.left ≠ null then
q.enqueue(node.left)
if node.right ≠ null then
q.enqueue(node.right)
No comments:
Post a Comment