/* 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; } };
之字形打印二叉树
猜你喜欢
转载自blog.csdn.net/runner668/article/details/80751266
今日推荐
周排行