一:题目
二:上码
class Solution {
public : vector< vector< int >> levelOrderBottom ( TreeNode* root) { vector< vector< int > > ans; stack< vector< int > > s; queue< TreeNode * > q; if ( root != NULL ) q. push ( root) ; while ( ! q. empty ( ) ) { int size = q. size ( ) ; vector< int > v; for ( int i = 0 ; i < size; i++ ) { TreeNode* ptr = q. front ( ) ; q. pop ( ) ; v. push_back ( ptr-> val) ; if ( ptr-> left != NULL ) q. push ( ptr-> left) ; if ( ptr-> right != NULL ) q. push ( ptr-> right) ; } s. push ( v) ; v. clear ( ) ; } while ( ! s. empty ( ) ) { ans. push_back ( s. top ( ) ) ; s. pop ( ) ; } return ans; }
} ;
三
二:上码
class Solution {
public : vector< vector< int >> levelOrder ( TreeNode* root) { vector< vector< int > > ans; queue< TreeNode* > q; if ( root != NULL ) { q. push ( root) ; } while ( ! q. empty ( ) ) { int size = q. size ( ) ; vector< int > v; for ( int i = 0 ; i < size; i++ ) { TreeNode* ptr = q. front ( ) ; q. pop ( ) ; v. push_back ( ptr-> val) ; if ( ptr-> left != NULL ) q. push ( ptr-> left) ; if ( ptr-> right != NULL ) q. push ( ptr-> right) ; } ans. push_back ( v) ; v. clear ( ) ; } return ans; }
} ;