How would you print out the data in a binary tree, level by
level, starting at the top?

Answer Posted / sucharit

This is the C# version

private void PrintLevelOrder(BinaryTreeNode node)
{
// Do a level Order Traversal

Queue<BinaryTreeNode> queue = new
Queue<BinaryTreeNode>();

queue.Enqueue(node);

while (queue.Count != 0)
{

Console.WriteLine((node = queue.Dequeue
() as BinaryTreeNode).IntValue.ToString());

if (node.Left !=null)
queue.Enqueue(node.Left as
BinaryTreeNode);
if (node.Right!=null)
queue.Enqueue(node.Right as
BinaryTreeNode);


}


}

Is This Answer Correct ?    3 Yes 1 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Who invented b language?

919


What does sizeof int return?

594


What is the meaning of && in c?

550


What is d'n in c?

638


What is the purpose of the following code? Is there any problem with the code? void send(int count, short *to, short *from) { /* count > 0 assumed */ register n = (count + 7) / 8; switch (count % 8) { case 0: do { *to = *from++; case 7: *to = *from++; case 6: *to = *from++; case 5: *to = *from++; case 4: *to = *from++; case 3: *to = *from++; case 2: *to = *from++; case 1: *to = *from++; } while (--n > 0); } }

1960






Is main an identifier in c?

605


Do you know what are bitwise shift operators in c programming?

588


If a five digit number is input through the keyboard, write a program to print a new number by adding one to each of its digits.For example if the number that is input is 12391 then the output should be displayed as 23402

3249


What is the difference between new and malloc functions?

581


What is the use of a static variable in c?

595


How can I dynamically allocate arrays?

594


Are bit fields portable?

680


What is the use of the function in c?

599


What is Dynamic memory allocation in C? Name the dynamic allocation functions.

559


What is call by reference in functions?

569