/* 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) { stack<TreeNode*> s1; // 奇数层节点 stack<TreeNode*> s2; // 偶数层节点 vector<vector<int> > res; if(pRoot) s1.push(pRoot); while(s1.size() || s2.size()) { vector<int> ans; // 每一层的节点的值 while(s1.size()) { auto t = s1.top(); s1.pop(); ans.push_back(t->val); // 将节点值压入ans中 if(t->left) s2.push(t->left); if(t->right) s2.push(t->right); } if(ans.size()) res.push_back(ans); // 将奇数层数据压入res ans.clear(); // 清空ans中的数据 while(s2.size()) { auto t = s2.top(); s2.pop(); ans.push_back(t->val); if(t->right) s1.push(t->right); if(t->left) s1.push(t->left); } if(ans.size()) res.push_back(ans); // 将偶数层节点的值压入res; } return res; } };