"Leetcode-Interview algorithm classic-java implementation" "102-binary Tree Level order traversal (binary sequence Traversal)"

Source: Internet
Author: User

"102-binary Tree Level Order traversal (binary tree sequence traversal)" "leetcode-Interview algorithm classic-java Implementation" "All topics Directory Index" Original Question

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]]

Main Topic

Given a binary tree, output the nodes of each of its layers.

Thinking of solving problems

Use two queues, one to save the current processing layer, and one to save the next one to process. It's all done at every level.

Code Implementation

Tree Node class

publicclass TreeNode {    int val;    TreeNode left;    TreeNode right;    TreeNode(int x) {        val = x;    }}

Algorithm implementation class

Import Java.util.arraylist;import java.util.deque;import Java.util.linkedlist;import Java.util.List;public class Solution {public list<list<integer>> levelorder (TreeNode root) {list<list<integer>> re Sult = new arraylist<> ();if(root = null) {returnResult        } deque<treenode> cur = new linkedlist<> (); Deque<treenode> Sub = new linkedlist<> ();Deque<treenode> exc;        TreeNode node; Cur.addlast (root); while(!cur.isempty ()) {list<integer> layout = new linkedlist<> (); while(!cur.isempty ())                {node = Cur.removefirst (); Layout.add (Node.val);if(Node.left! = null) { Sub. AddLast (node.left);}if(node.right! = null) { Sub. AddLast (node.right);}} exc = cur; Cur = Sub;             Sub = exc;Result.add (layout); }returnResult }}
Evaluation Results

  Click on the picture, the mouse does not release, drag a position, release after the new window to view the full picture.

Special Instructions Welcome reprint, Reprint please indicate the source "http://blog.csdn.net/derrantcm/article/details/47354349"

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

leetcode-Interview Algorithm classic-java implementation "102-binary Tree Level order traversal (binary sequence 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.