Leetcode-102 Binary Tree Level Order Traversal (layered Traversal of Binary Trees), leetcodetraversal

Source: Internet
Author: User

Leetcode-102 Binary Tree Level Order Traversal (layered Traversal of Binary Trees), leetcodetraversal
Binary Tree Level Order Traversal Total Accepted: 51429 Total Submissions: 174478
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:
[
[3],
[9, 20],
[15, 7]
]
Confused what "{1, #, 2, 3}" means? > Read more on how binary tree is serialized on OJ.


Hide Tags: Tree Breadth-first Search (Breadth-first traversal)


Solution:
It traverses the breadth and records each layer. Breadth traversal is implemented by queue and record is implemented by list

Use a queue and a list. Queues are used to record each layer of nodes. The list is used to store nodes of each layer.


/** * Definition for a binary tree node. * public class TreeNode  * { *     int val; *     TreeNode left; *     TreeNode right; *     TreeNode(int x) { val = x; } * } */

Public List <Integer> levelOrder (TreeNode root) {List <Integer> list = new partition List <Integer> (); // if the root node is empty, listif (root = null) {return list;} // create a Queue <TreeNode>, used to store all the node Queue <TreeNode> currentlevel = new Queue list <TreeNode> (); currentlevel. add (root); while (! Currentlevel. isEmpty () {// create a List <Integer> to record the Value List of all nodes in the current layer <Integer> currentList = new unknown List <Integer> (); // size indicates the number of nodes at the current layer. int size = currentlevel. size (); for (int I = 0; I <size; I ++) {// poll () Get and remove the header of this list (the first element) treeNode currentNode = currentlevel. poll (); // record the node values of each layer to currentList in List <Integer>. add (currentNode. val); if (currentNode. left! = Null) {currentlevel. add (currentNode. left);} if (currentNode. right! = Null) {currentlevel. add (currentNode. right) ;}}// Add the current List <Integer> to List <Integer>. add (currentList);} return list ;}


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.