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