Leetcode OJ 107. Binary Tree level Order traversal II

Source: Internet
Author: User

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

Return its bottom-up level order traversal as:

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

Confused what "{1,#,2,3}" means? > read more on how binary tree is serialized on OJ.

1 /**2 * Definition for a binary tree node.3 * public class TreeNode {4 * int val;5 * TreeNode left;6 * TreeNode right;7 * TreeNode (int x) {val = x;}8  * }9  */Ten  Public classSolution { One      PublicList<list<integer>>levelorderbottom (TreeNode root) { AList<list<integer>> List =NewLinkedlist<list<integer>>(); -         if(Root = =NULL)returnlist; -          theQueue<treenode> queue1 =NewLinkedlist<treenode>(); -Queue<treenode> queue2 =NewLinkedlist<treenode>(); -          - Queue1.offer (root); +          -          while(Queue1.size () >0){ +List<integer> childlist =NewArraylist<integer>(); A              while(Queue1.size () >0){ atTreeNode node =Queue1.remove (); - Childlist.add (node.val); -                 if(node.left!=NULL) Queue2.offer (node.left); -                 if(node.right!=NULL) Queue2.offer (node.right); -             } -List.add (0, childlist); inqueue<treenode> temp =queue1; -Queue1 =queue2; toQueue2 =temp; +         } -          the         returnlist; *     } $}

Leetcode OJ 107. Binary Tree level Order 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.