"Leetcode" 107. Binary Tree level Order traversal II problem Solving report

Source: Internet
Author: User

Reprint Please specify source: http://blog.csdn.net/crazy1235/article/details/51508308

Subject

Source: https://leetcode.com/problems/binary-tree-level-order-traversal-ii/

Given a binary tree,return  theBottom-up Level Order Traversal of  itsNodes ' values. (ie, fromLeft toRight, Level byLevel fromLeaf toRoot). For Example:given binary Tree {3,9, -,#,#,15,7},    3/9   -/ the   7return  itsBottom-up Level Order Traversal as:[  [ the,7],  [9, -],  [3]]
Explain

Sequence traversal, which starts at the bottom and goes up by the layer output.
The topic with "102. Binary Tree level Order traversal "similar. Just the opposite.

Solution

In fact, the most convenient way to do this is to reverse the output of the sequence.
Use "102. Binary Tree Level Order Traversal "method, get list after, then call collections.reverse (list); the method reverses it.

Solution 1

The non-recursive method in the "102" topic is to use the queue and then add the results of each layer to the ArrayList.

Change the ArrayList to LinkedList, and then add the result of one layer at a time to the head, which is called the AddFirst () method.

    /** * 3ms <br/> * Beats 34.33% of java submissions * * @author Jacksen * @param root * @return * /     Public List<List<Integer>>Levelorderbottom (TreeNode root) {LinkedList<List<Integer>>Result= NewLinkedList<List<Integer>>();if(Root== NULL) {returnResult }Queue<TreeNode> Queue = NewLinkedList<TreeNode>();Queue.Add (root); int I= Queue.Size ();//Record the number of nodes per layerTreeNode Tempnode= NULL;List<Integer>Singlelevel= NewArrayList<>(); while(!Queue.IsEmpty ()) {if(I== 0) {//One level record end                //Result.AddFirst (Singlelevel); I= Queue.Size (); Singlelevel= NewArrayList<>(); } Tempnode= Queue.Poll (); Singlelevel.Add (Tempnode.Val);--Iif(Tempnode.Left!= NULL) {Queue.Add (Tempnode.left); }if(Tempnode.Right!= NULL) {Queue.Add (Tempnode.right); }} Result.AddFirst (Singlelevel);returnResult }

The Leetcode platform Run time is 3ms .

Solution 2

Recursive mode

Method Two can still be in accordance with the "102" topic in the recursive method to modify it.

    /** * Recursive way <br/> * Important is record level <br/> * 2ms<br/> * eats81.17% of Java Submissions * * @param root * @return * *     PublicList<list<integer>>LevelOrderBottom2(TreeNode Root) {linkedlist<list<integer>> result =NewLinkedlist<list<integer>> (); Levelrecursion (root, result,0);returnResult }/** * Recursive method * /    Private void levelrecursion(TreeNode node, linkedlist<list<integer>> result,intLevel) {if(node = =NULL) {return; }if(Result.size () < level +1) {//Description need to add one more lineResult.addfirst (NewArraylist<integer> ()); } result.get (Result.size ()-1-level). Add (Node.val); Levelrecursion (Node.left, result, level +1); Levelrecursion (node.right, result, level +1); }

The Leetcode platform Run time is 2ms .

So easy~~

"Leetcode" 107. Binary Tree level Order traversal II problem Solving report

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.