BM27. 按之字形顺序打印二叉树
描述
示例1
输入:
{1,2,3,#,#,4,5}
输出:
[[1],[3,2],[4,5]]
说明:
如题面解释,第一层是根节点,从左到右打印结果,第二层从右到左,第三层从左到右。示例2
输入:
{8,6,10,5,7,9,11}
输出:
[[8],[10,6],[5,7,9,11]]
示例3
输入:
{1,2,3,4,5}
输出:
[[1],[3,2],[4,5]]
C++ 解法, 执行用时: 2ms, 内存消耗: 360KB, 提交时间: 2021-06-08
/* 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>> ret; if (!pRoot) return ret; queue<TreeNode*> q; q.push(pRoot); int level = 0; while (!q.empty()) { int sz = q.size(); vector<int> ans; while (sz--) { TreeNode *node = q.front(); q.pop(); ans.push_back(node->val); if (node->left) q.push(node->left); if (node->right) q.push(node->right); } ++level; if (!(level&1)) // 偶数层 反转一下 reverse(ans.begin(), ans.end()); ret.push_back(ans); } return ret; } };
C++ 解法, 执行用时: 2ms, 内存消耗: 360KB, 提交时间: 2021-06-02
/* 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; if(pRoot == nullptr) return res; int dir=1; stack<TreeNode*> st; queue<TreeNode*> q; st.push(pRoot); while(!st.empty()) { vector<int> v; int sz=st.size(); while(sz--) { TreeNode* top=st.top(); st.pop(); v.push_back(top->val); TreeNode* first=(dir == 1) ? top->left : top->right; TreeNode* second=(dir == 1) ? top->right : top->left; if(first != nullptr) q.push(first); if(second != nullptr) q.push(second); } res.push_back(v); while(!q.empty()) { st.push(q.front()); q.pop(); } dir=(dir == 1) ? 2 : 1; } return res; } };
C++ 解法, 执行用时: 2ms, 内存消耗: 360KB, 提交时间: 2021-05-30
/* 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) { if(!pRoot)return {}; queue<TreeNode*>que; vector<vector<int>>res; que.push(pRoot); int floor=0; while(!que.empty()){ int size=que.size(); floor++; vector<int>vec; for(int i=0;i<size;i++){ auto it=que.front(); que.pop(); vec.push_back(it->val); if(it->left)que.push(it->left); if(it->right)que.push(it->right); } if(floor%2==0){ std::reverse(vec.begin(), vec.end()); } res.push_back(vec); } return res; } };
C++ 解法, 执行用时: 2ms, 内存消耗: 360KB, 提交时间: 2021-03-22
/* struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) { } }; */ class Solution { vector<vector<int> > res; void BFS(TreeNode* pRoot) { queue<TreeNode*> q; q.push(pRoot); int l=q.size(); vector<int> now; int flag=0; while(!q.empty()) { TreeNode* tmp=q.front(); q.pop(); --l; now.push_back(tmp->val); if(tmp->left) q.push(tmp->left); if(tmp->right) q.push(tmp->right); if(l==0) { l=q.size(); if(flag==1) { reverse(now.begin(),now.end()); } flag=1-flag; res.push_back(now); now.clear(); } } } public: vector<vector<int> > Print(TreeNode* pRoot) { if(pRoot==nullptr) return res; BFS(pRoot); return res; } };
C++ 解法, 执行用时: 2ms, 内存消耗: 360KB, 提交时间: 2021-03-22
/* 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<int> assit; //辅助数组,存储每层节点个数 vector<int> nums; //存储层序遍历的二叉树元素 vector<vector<int>> results; int level = 0; if(pRoot == nullptr){ return results; } queue<TreeNode *> que;//辅助队列 que.push(pRoot); //求出深度和所有节点的层序元素 while(!que.empty()){ level++; int size = que.size(); assit.push_back(size); for(int i = 0; i < size; ++i){ TreeNode *front = que.front(); nums.push_back(front->val); que.pop(); if(front->left) que.push(front->left); if(front->right) que.push(front->right); } } //进行之字形输出 int sizeOfLevel = 0; int index = 0; for(int index_level = 1; index_level <= level; ++index_level){ sizeOfLevel = assit[index_level - 1]; if(index_level % 2 == 1){ vector<int> result_level; int index_temp = index; while(index < index_temp + sizeOfLevel){ result_level.push_back(nums[index]); ++index; } results.push_back(result_level); }else{ vector<int> result_level; for(int i = index + sizeOfLevel - 1; i >= index; --i){ result_level.push_back(nums[i]); } index += sizeOfLevel; results.push_back(result_level); } } return results; } };