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]]
Analysis: The code is as follows:
classSolution { Public: Vector<vector<int> > Levelorderbottom (TreeNode *root) {Vector<vector<int> >result; if(Root = NULL)returnresult; Vector<vector<treenode *> >levels; Vector<treenode *> Prev (1, Root); while(!Prev.empty ()) {Levels.push_back (prev); Vector<treenode *>cur; for(Auto i = Prev.begin (); I! = Prev.end (); i++){ if((*i)->left) Cur.push_back ((*i)Left ); if((*i)->right) Cur.push_back ((*i)Right ); } prev=cur; } for(Auto i = Levels.rbegin (); I! = Levels.rend (); i++) {vector<int>tmp; for(Auto J = I->begin (); J! = I->end (); j + +) Tmp.push_back (*J)val); Result.push_back (TMP); } returnresult; }};
Leetcode:binary Tree level Order traversal II