LeetCode102 Binary Tree level Order traversal Java

Source: Internet
Author: User

Title:
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  20    /     15   7

Return its level order traversal as:

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

Analysis:
The hierarchy traverses the binary tree. is to first visit the first element of the binary tree, then visit the second level, and then visit the third tier. And so on

The way to do this is to use a FIFO queue as the secondary data structure. Save the elements of each layer with levellist, save all the levellist with Resultlist, and
(1) Put the root node into the queue. and put a sentinel node into the queue. Sentinel nodes are used to identify a layer that has ended
(2) When the number of elements in the queue is greater than 1 o'clock (there are other elements besides the Sentinel node). Into the loop.

To access the element, assume that the element is a sentinel node. It means that the layer is over, and a sentinel node is enqueued to identify the place where the next layer ends and to deposit the levellist into the resultlist. and build a new levellist to save the next layer of elements; otherwise, put the value of the node into Levellist and queue it with a child node that is not null.
(3) Add levellist to Resultlist. Since the last sentinel node was not able to be interviewed, the levellist that saved the last layer of elements had no way of being added to the resultlist in the loop.

The detailed code such as the following:

 PackageGlobal Public  class TreeNode {     Public intVal PublicTreeNode left; PublicTreeNode right; Public TreeNode(intX) {val = x; }@Override     PublicStringtoString() {return "TreeNode [val="+ val +", left="+ Left +", right="+ Right +"]"; }}
Package leetcode102;Import Global.TreeNode;ImportJava.Util.ArrayList;ImportJava.Util.LinkedList;ImportJava.Util.List;/** * Created by Liyuncong on 10/22/15. * * PublicClass LeetCode102 { Public List<List<Integer>>Levelorder (TreeNode root) {List<List<Integer>>Resultlist= NewArrayList<List<Integer>>();if(Root== NULL) {returnResultlist; }List<Integer>Levelstorage= NewLinkedList<Integer>(); LinkedList<TreeNode> Queue = NewLinkedList<TreeNode>();Queue.Offer (root);Queue.OfferNULL); while(Queue.Size ()> 1) {TreeNode Top= Queue.Poll ();if(Top== NULL) {Resultlist.Add (Levelstorage);Queue.OfferNULL); Levelstorage= NewLinkedList<Integer>(); }Else{Levelstorage.Add (Top.Val);if(Top.Left!= NULL) {Queue.Offer (top.left); }if(Top.Right!= NULL) {Queue.Offer (top.right); }}} Resultlist.Add (Levelstorage);returnResultlist; }}

LeetCode102 Binary Tree level Order traversal Java

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.