Leetcode: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]]
The subject is very interesting. is to get you from the tail to the top, from left to right, and then output a container that is a <int> container.
1 /**2 * Definition for binary tree3 * struct TreeNode {4 * int val;5 * TreeNode *left;6 * TreeNode *right;7 * TreeNode (int x): Val (x), left (null), right (null) {}8  * };9  */Ten classSolution { One  Public: Avector<vector<int> > out;//Create a new container to return -     voidLeveltraval (vector<treenode* >&Level ) -     { the         if(Level.size () >0) -         { -Vector<treenode*>Nextlevel; -              for(inti =0; I < level.size (); i++) +             { -                 if(level[i]->Left ) +Nextlevel.push_back (level[i]->Left ); A                 if(level[i]->Right ) atNextlevel.push_back (level[i]->Right ); -             } - Leveltraval (nextlevel); -vector<int>Val; -              for(inti =0; I < level.size (); i++) -Val.push_back (level[i]->val); in              out. Push_back (val); -         } to     } +vector<vector<int> > Levelorderbottom (TreeNode *root) { -         //Start Typing your/C + + solution below the         //Do not write int main () function *          out. Clear (); $Vector<treenode*>v;Panax Notoginseng         if(Root! =NULL) -         { the V.push_back (root); + Leveltraval (v); A         } the         return  out; +     } -};

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.