/*
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> > res;
vector<int> res0;
if(pRoot == NULL)
return res;
std::stack<TreeNode *> stack[2];
int current = 0;
int next = 1;
stack[current].push(pRoot);
while(!stack[0].empty()||!stack[1].empty())
{
TreeNode * pNode =stack[current].top();
stack[current].pop();
res0.push_back(pNode->val);
if(current ==0)
{
if(pNode->left!=NULL)
stack[next].push(pNode->left);
if(pNode->right!=NULL)
stack[next].push(pNode->right);
}
else{
if(pNode->right!=NULL)
stack[next].push(pNode->right);
if(pNode->left!=NULL)
stack[next].push(pNode->left);
}
if(stack[current].empty())
{
res.push_back(res0);
current = 1-current;
next = 1-next;
res0.clear();
}
}
return res;
}
};之字形打印二叉树
最新推荐文章于 2020-06-16 19:47:27 发布
878

被折叠的 条评论
为什么被折叠?



