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 hierarchical traversal of the binary tree is the first to access the first element of the binary tree, then access the second layer, then access the third layer, and so on. The way to do this is to use a FIFO queue as a secondary data structure, save the elements of each layer with Levellist, save all levellist with Resultlist, and then
(1) The root node into the queue, and a sentinel node into the queue, sentinel node used to identify a layer has ended
(2) When the number of elements in the queue is greater than 1 o'clock (other elements besides the Sentinel node), enter the loop. Access to this element, if the element is a sentinel node, then this layer is finished, and a sentinel node is queued to identify the next layer to the end of the place, the Levellist deposit resultlist, and a new levellist to save the next layer of elements; 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 has no way to be accessed, the levellist that saves the last layer of elements cannot be added to the resultlist in the loop.

The specific code is as follows:

 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.