Binary tree printing by layer, and wrapping by layer method

Source: Internet
Author: User

Often some are printed by the layer of binary tree, the key to such problems is, when to break the problem.

This question in the cattle and the network of the explanation let me head very big, also do not understand very much. It happens that the sword refers to the offer inside there is this problem, the inside of the resolution is clearly more.

First construct a queue, set two properties a nowline is initialized to 0, which is used to save the number of elements of the currently printed line that are not yet printed.

Set another variable nextline variable to hold the total number of elements in the next row, initialized to 0.

For example,

First step: Create a new queue, set nowline=1,nextline=0,.

The second step: the head node of the content a into the queue, this time from the queue pop-up elements, the nowline minus 1, a two sub-tree content successively added to the queue, each join a queue, to nextline plus 1

The third step: detect nowline is not equal to 0, if not equal to zero, continue to eject, perform the second step after the content, if equal to 0, then let Nowline=nextline, and let nextline=0, and print a newline;

Fourth step: And so on until there is no content in the queue

Dig the pits, then write the code ...

Binary tree printing by layer, and wrapping by layer method

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.