[Leetcode] Binary Tree level Order traversal II

Source: Internet
Author: User

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

Return its bottom-up level order traversal as:

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

1. Classification of types of questions:

2, the idea: first calculate the depth, recursively add each point, according to the depth, determine where the linked list

3. Complexity of Time:

4. Code:

 Public classSolution { PublicList<list<integer>>levelorderbottom (TreeNode root) {List<List<Integer>> list=NewArraylist<list<integer>>(); if(root==NULL)returnlist; intDepth=depth (root);  for(inti=0;i<depth;i++) {List<Integer> temp=NewArraylist<integer>();        List.add (temp); } traverdepth (Root, List,1, depth);        Collections.reverse (list); returnlist; }     Public voidTraverdepth (TreeNode node,list<list<integer>> List,intCurdepth,intdepth) {                if(node!=NULL{List.get (curDepth-1). Add (Node.val);}Else return; Curdepth++; if(node.left!=NULL) traverdepth (Node.left, list, curdepth, depth); if(node.right!=NULL) traverdepth (node.right, list, curdepth, depth); }     Public intdepth (TreeNode root) {if(root==NULL){              return0; }          return1 +Math.max (Depth (root.left), Depth (root.right)); } }

5. Optimization:

  Some methods on the Internet use two auxiliary linked list, the first record when the layer of node, the second record all the next layer of node, after traversing, will be the first to return to the total linked list, when the layer points to the next layer, the next layer is empty, so continue to iterate down.

  

List<list<integer>> list=NewArraylist<list<integer>>(); if(root==NULL)returnlist; List <TreeNode> listcur=new arraylist<treenode>(); List<TreeNode> listnex=new arraylist<treenode>(); Listcur.add (root);//Add First         while(!Listcur.isempty ()) {List<Integer> listtemp=NewArraylist<integer>();  for(intI=0;i<listcur.size (); i++) {TreeNode TreeNode=Listcur.get (i);                Listtemp.add (Treenode.val); if(treenode.left!=NULL) Listnex.add (treenode.left); if(treenode.right!=NULL) Listnex.add (treenode.right);                                    } list.add (Listtemp); List<TreeNode> temp=listcur; Listcur=Listnex; Listnex=temp;        Listnex.clear ();        } collections.reverse (list); returnList

6. Expansion

  Order

 Public classSolution { PublicList<list<integer>>Levelorder (TreeNode root) {List<List<Integer>> list=NewArraylist<list<integer>>(); if(root==NULL)returnlist; List<TreeNode> listcur=NewArraylist<treenode>(); List<TreeNode> listnex=NewArraylist<treenode>(); Listcur.add (root);//Add First         while(!Listcur.isempty ()) {List<Integer> listtemp=NewArraylist<integer>();  for(intI=0;i<listcur.size (); i++) {TreeNode TreeNode=Listcur.get (i);                Listtemp.add (Treenode.val); if(treenode.left!=NULL) Listnex.add (treenode.left); if(treenode.right!=NULL) Listnex.add (treenode.right);                                    } list.add (Listtemp); List<TreeNode> temp=listcur; Listcur=Listnex; Listnex=temp;        Listnex.clear (); }        returnlist; }}

[Leetcode] 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.