"Leetcode-Interview algorithm classic-java implementation" "107-binary Tree Level Order Traversal II (binary sequence Traversal II)"

Source: Internet
Author: User

"107-binary Tree Level Order Traversal II (binary tree Sequence Traversal II)" "leetcode-Interview algorithm classic-java Implementation" "All topics Directory Index" Original Question

Given a binary tree, return the bottom-up level order traversal of its nodes ' values. (ie, from the left-to-right, the level by level from the leaf to root).
For example:
Given binary Tree {3,9,20,#,#,15,7},

    3     9  20    /     15   7

Return its bottom-up level order traversal as:

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

Main Topic

Given a binary tree, the sequence traversal is done from the bottom down.

Thinking of solving problems

The tree is sequentially traversed, and the results of each layer are placed on the head of the resulting list.

Code Implementation

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

Algorithm implementation class

ImportJava.Util.*; PublicClass Solution { Public List<List<Integer>>Levelorderbottom (TreeNode root) {List<List<Integer>> List = NewLinkedList<>();if(Root== NULL) {return List; } Deque<TreeNode>Cur= NewLinkedList<>(); Deque<TreeNode>Nxt= NewLinkedList<>(); Deque<TreeNode>Exc= NewLinkedList<>();        TreeNode tmp; Cur.Add (root); while(!Cur.IsEmpty ()) {List<Integer>Layout= NewArrayList<>(); while(!Cur.IsEmpty ()) {tmp=Cur.Remove ();if(TMP.Left!= NULL) {NXT.Add (tmp.left); }if(TMP.Right!= NULL) {NXT.Add (tmp.right); } layout.Add (tmp.Val); } exc=Cur Cur=Nxt Nxt=ExcList.Add0, layout); }return List; }}
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/47392965"

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

"Leetcode-Interview algorithm classic-java implementation" "107-binary Tree Level Order Traversal II (binary sequence Traversal II)"

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.