The sword refers to the offer surface question (Java edition): Print binary tree from top down

Source: Internet
Author: User

Title: Each node of the binary tree is printed from top to bottom, and the nodes in the same layer are printed in order from left to right. For example, if you enter a two-fork tree, the 8,6,10,5,7,9,11 is printed once.


This problem essentially examines the tree traversal algorithm, but this traversal is not familiar with the pre-order, middle order or post-sequence traversal. Since we are not familiar with this method of traversing by layers, we may have downloaded and wondered about the traversal process.

Because the order in which layers are printed determines the root node that should be printed first, we start with the root node of the tree. In order to be able to print two sub-nodes of the 8 node, we should save two nodes with a value of 6 and 10 in a container as we traverse to the node, and now there are two nodes in the container. In order to print from left to right, we first take out a node with a value of 6. Print out 6 and put its value at 5 and 7 for each of the two nodes into the data container. At this point, there are three nodes in the data container with values of 10,5,7. Next we take the node with a value of 10 from the data container. Notice that the node value of 10 is 5, 7, the node is placed in the container first, and then the two nodes are removed first, this is what we usually say first in first out, so it is not difficult to see that this container should be a queue. Because nodes with a value of 5,7,9,11 have no child nodes, they can be printed in turn.

By analyzing specific examples, we can find the rules for printing binary trees from top to bottom: each time a node is printed, if the node has child nodes, the node's child nodes are placed at the end of a queue. Then go to the head of the queue and take the first node that goes into the queue, repeating the previous print operation until all the nodes in the queue are printed out.

Java Code Implementation:

/** * Prints Each node of the two-fork tree from top to bottom, and the nodes of the same layer are printed in order from left to right. */package swordforoffer;import java.util.linkedlist;import java.util.queue;import utils. binarytreenode;/** * @author Jinshuangqi * * August 3, 2015 */public class E23printfromtoptobottom {public <E> void   p Rintfromtoptobottom (Binarytreenode root) {if (root = null) return; queue<binarytreenode> queue = new linkedlist<binarytreenode> (); Queue.add (root); while (!queue.isempty ()) {Binarytreenode node = Queue.poll (); System.out.print (node.value+ ","); if (node.leftnode! = null) Queue.add (Node.leftnode); if (node.rightnode! = null) Queue.add (Node.rightnode);}}}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

The sword refers to the offer surface question (Java edition): Print binary tree from top down

Related Article

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.