Binary Tree Level Order Traversal II


  • 0
    A

    '''
    class Solution {
        public:
               vector<vector<int>> levelOrderBottom(TreeNode* root) {
                         vector<vector<int>> order;
                         if(root==NULL) return order;
                         queue<TreeNode> q;
                         q.push(*root);
                         while(!q.empty()){
                             int size=q.size();
                             vector<int> tmp;
                             for(int i=0;i<size;i++){
                                tmp.push_back(q.front().val);
                                TreeNode node=q.front(); q.pop();
                                if(node.left!=NULL) q.push(*node.left);
                                if(node.right!=NULL) q.push(*node.right);
                              }
                             order.push_back(tmp);
                         }
                         reverse(order.begin(),order.end());
                         return order;
               }
    };

    '''


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.