题目描述
blablabla
样例
blablabla
算法1
用两个栈做的
C++ 代码
#include<queue>
class Solution {
public:
stack<TreeNode*> a, b;
vector<vector<int>> dat;
vector<vector<int>> printFromTopToBottom(TreeNode* root) {
a.push(root);
while (!a.empty() || !b.empty()) {
vector<int> temp;
while (!a.empty()) {
if (a.top()) {
auto x = a.top();
temp.push_back(x->val);
b.push(x->left);
b.push(x->right);
}
a.pop();
}
if (!temp.empty()) {
dat.push_back(temp);
}
temp.clear();
while (!b.empty()) {
if (b.top()) {
auto x = b.top();
temp.push_back(x->val);
a.push(x->right);
a.push(x->left);
}
b.pop();
}
if (!temp.empty()) {
dat.push_back(temp);
}
}
return dat;
}
};