Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

how can i print a binary tree level by level

can someone help me out in printing a binary tree level by level with/without using recursion
[93 byte] By [mchandel] at [2007-11-11 8:29:58]
# 1 Re: how can i print a binary tree level by level
Hi,

try something like the following:
when traversing the tree maintain a queue in which you push the subnodes of your current node to the end. then take the first node in your queue, display the contents and remove it from the queue and loop... I guess that will do what you want?

Cheers,

D
drkybelk at 2007-11-11 21:01:18 >
# 2 Re: how can i print a binary tree level by level
By avoiding recursion this is gonna get ugly no matter how you slice it up. Do you have any info at all (depth, for example) or is it all "on the fly"?
jonnin at 2007-11-11 21:02:23 >