/*
struct TreeNode {
    int val;
    struct TreeNode *left;
    struct TreeNode *right;
    TreeNode(int x) :
            val(x), left(NULL), right(NULL) {
    }
};
*/
class Solution {
public:
    vector<vector<int> > Print(TreeNode* pRoot) {
        vector<vector<int> > result;
        if (pRoot == NULL) return result;
        deque<TreeNode *> que;
        que.push_back(pRoot);
        int level = 0;
        while(!que.empty()) {
            int len = que.size();
            vector<int> levelNode;
            while(len--) {
                if (level&1) {
                    TreeNode* node = que.front();
                    que.pop_front();
                    levelNode.push_back(node->val);
                    if (node->right)  que.push_back(node->right);
                    if (node->left) que.push_back(node->left);
                } else {
                    TreeNode* node = que.back();
                    que.pop_back();
                    levelNode.push_back(node->val);
                    if (node->left)  que.push_front(node->left);
                    if (node->right) que.push_front(node->right);
                }
            }
            ++ level;
            result.push_back(levelNode);
        }
        return result;
    }  
};