Binary Tree level Order traversal

Source: Internet
Author: User

This article is in the study summary, welcome reprint but please specify Source:http://blog.csdn.net/pistolove/article/details/41929059


Given a binary tree, return the level order traversal of its nodes ' values. (ie, from left-to-right, level by level).

For example:
Given binary Tree {3,9,20,#,#,15,7} ,

    3   /   9    /   7

Return its level order traversal as:

[  3],  [9,20],  [15,7]]

Ideas:

(1) Test instructions for a given binary tree, output the corresponding value of each layer at the level.

(2) This article mainly uses the queue to operate each layer node in the tree. To avoid traversal from the root node each time, when we access the ground level m layer, we only need to know the M-1 layer node information, so that you do not have to start from the root node each time to access, thereby improving efficiency.

(3) Here set two marker bits first and last to record the location of the node to be accessed by the current layer and the position of the next node in the final node of the current layer.

(4) The idea of solving the problem is as follows: if the root node is not empty, the root node is added to the current queue, the initialization tag bit first is 0, that is, the current queue to be accessed in the second node, the initialization tag bit last is 1, that is, the number of elements in the current queue. If first is smaller than the queue size, that is, the node in the queue that corresponds to the primary position exists, then the loop prepares to traverse the level one, last points to the next position in the final element of the queue, and if First<last, indicates that the layer has elements that are not accessed. The node corresponding to the first position is stored in the list of nodes in the temporary storage node, if the left and right nodes are not empty, then the left and right nodes are stored in the current linked list, and a node is traversed, first++, that is, a node to be traversed, loop until first==last, This indicates that the current layer node is all traversed, the temporary list is stored in the queue to be output, then the second layer is traversed, and so on, until the last node in the final layer is traversed, the last equals the number of the current queue element, jumps out of the loop, and returns the result.

(5) For the example in the topic, the following is the execution process:

A: The root node 3 is added to the current queue all, initialize the First=0,last=1

B: Because first<all.size () = 1, the location of the first corresponds to the node in all, at which point the last execution of all node 3 at position 0 of the next position 1

C: Due to first<last, the current layer does not traverse, take out first to the element 3 in the temporary linked list level, continue to see the node of the lower child node, that is, whether its left and right node is empty

D: NOT NULL to join 9 and 20 into the queue, at this point, the first point element has been traversed, firstly points to the next element, first++, at this time first=last, indicating that the layer has been traversed

E: Then traverse the second layer, the second layer start position is 1, that is, from node 9 to start traversing, ...

F: It is important to note that, each traversal to a node, the left and right nodes to join the queue must be first left and back, in order to ensure the correctness of the output sequence.

(6) This article mainly examines the application of the queue, especially its "FIFO" characteristics, to ensure that each layer is left-to-right in order to output.

(7) Hope this article is helpful to you. Thank you.


The algorithm code is implemented as follows:

public static list<list<integer>> Levelorder (TreeNode root) {list<list<integer>> result = new Linkedlist<list<integer>> ();//Note that root is empty and cannot be returned nullif (root = null) return result; list<treenode> all = new linkedlist<treenode> (); All.add (root); int first = 0; The current node to be accessed, initially the first node, that is, the root node int last = 1; The number of elements in the current list, initially only one while (first < All.size ()) {  //If the node to be accessed exists in the list last = All.size ();      The next line of access begins with the last node at the end of the current row list<integer> level = new linkedlist<integer> (); {    //If first==last indicates that all nodes of the row have been accessed, jump out of the Loop Level.add (All.get (first). val); if (All.get (first). Left! = null) {All.add ( All.get (first). left);} if (All.get (first). Right! = null) {All.add (All.get (first). right);} first++; One node at the end of each visit points to the next node}result.add (level);} return result;}




Binary Tree level Order traversal

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.