首页 > 代码库 > 剑指offer---按只字形顺序打印二叉树
剑指offer---按只字形顺序打印二叉树
class Solution { public: vector<vector<int> > Print(TreeNode* pRoot) { TreeNode* cur = pRoot; stack<TreeNode*> stack1_LeftToRight; stack<TreeNode*> stack2_RightToLeft; stack1_LeftToRight.push(pRoot); vector<vector<int> > result; if (pRoot == NULL) return result; while ((!stack1_LeftToRight.empty()) || (!stack2_RightToLeft.empty())) { vector<int> re; if( (!stack1_LeftToRight.empty()) && (stack2_RightToLeft.empty())) { while (!stack1_LeftToRight.empty()) { TreeNode* cur= stack1_LeftToRight.top(); if(cur->left!=NULL) stack2_RightToLeft.push(cur->left); if(cur->right!=NULL) stack2_RightToLeft.push(cur->right); re.push_back(cur->val); stack1_LeftToRight.pop(); } result.push_back(re); } else { while (!stack2_RightToLeft.empty()) { TreeNode* cur = stack2_RightToLeft.top(); if(cur->right!=NULL) stack1_LeftToRight.push(cur->right); if(cur->left!=NULL) stack1_LeftToRight.push(cur->left); re.push_back(cur->val); stack2_RightToLeft.pop(); } result.push_back(re); } re.clear(); } return result; } };
剑指offer---按只字形顺序打印二叉树
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。