s4553711
4/19/2017 - 2:38 PM

107.cpp

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector<vector<int>> levelOrderBottom(TreeNode* root) {
        vector<vector<int>> q;
        vector<int> row;
        queue<TreeNode*> d;
        TreeNode* tmp;
        if (root == NULL)
            return q;
        d.push(root);
        while(!d.empty()) {
            int size = d.size();
            while(size--) {
                tmp = d.front();
                row.push_back(tmp->val);
                d.pop();
                if (tmp->left != NULL)
                    d.push(tmp->left);
                if (tmp->right != NULL)
                    d.push(tmp->right);
            }
            q.insert(q.begin(), row);
            row.clear();
        }
        return q;
    }
};