102. The binary tree hierarchy traversal
class Solution { public: vector<vector<int>> levelOrder(TreeNode* root) { vector<vector<int>> res; if(root==nullptr) return res; queue<TreeNode*> que; que.push(root); while(!que.empty()){ int n = que.size (); // record the number of elements of the current layer vector<int> level; for(int i=0;i<n;i++){ TreeNode* tmp = que.front(); que.pop (); level.push_back(tmp->val); if(tmp->left) que.push(tmp->left); if(tmp->right) que.push(tmp->right); } res.push_back(level); } return res; } };
107. The binary tree hierarchy traversal II
Given a binary tree, the node returns its bottom-up value hierarchy traversal. (Ie, by physical layer to the layer from the leaf nodes of the root node, layer by layer traversal from left to right)
Method a: use of queues, one after each traversal, the traversal header using the interpolation result. Inserted into the head of a two-dimensional array, to achieve reverse order.
class Solution { public: vector<vector<int>> levelOrderBottom(TreeNode* root) { vector<vector<int>> res; if(root==nullptr) return res; queue<TreeNode*> que; que.push(root); while(!que.empty()){ int n = que.size(); vector<int> level; for(int i=0;i<n;i++){ TreeNode* tmp = que.front(); que.pop (); level.push_back(tmp->val); if(tmp->left) que.push(tmp->left); if(tmp->right) que.push(tmp->right); } res.insert(res.begin(),level); } return res; } };
16ms execution
Method II using a recursive method (implemented by recursive reverse)
class Solution { public: vector<vector<int>> levelOrderBottom(TreeNode* root) { vector<vector<int>> res; if(root==nullptr) return res; queue<TreeNode*> que; que.push(root); levelOrderBottom (which res); return res; } void levelOrderBottom(queue<TreeNode*> que,vector<vector<int>>& res){ if(que.empty()) return; vector<int> arr; queue <TreeNode *> queNext; // for storing the next layer node while (! que.empty ()) {// arr stored in the current layer, and the next layer of nodes stored in queNext. Then recursively, in the final push into the array arr in res TreeNode* tmp = que.front(); que.pop (); arr.push_back(tmp->val); if(tmp->left) queNext.push(tmp->left); if(tmp->right) queNext.push(tmp->right); } levelOrderBottom(queNext, res); res.push_back(arr); return; } };
103. Binary Tree zigzag traverse the level (zigzag traversal)
Method a: two stacks
Two respective stack data storage layer, then the advanced features plus the details about the order of the nodes has a stack (who can look at the specific code whoever) corresponds exactly to the Z-shaped zigZag access sequence:
右往左时右先入栈,左往右时,左先入栈。
class Solution { public: vector<vector<int>> zigzagLevelOrder(TreeNode* root) { vector<vector<int>> res; if(root==nullptr) return res; stack<TreeNode*> sta1,sta2; vector<int> arr; TreeNode* tmp = root; sta1.push(tmp); while(true){ while(!sta1.empty()){ tmp = sta1.top(); sta1.pop(); arr.push_back(tmp->val); if(tmp->left) sta2.push(tmp->left); if(tmp->right) sta2.push(tmp->right); } if(!arr.empty()){ res.push_back(arr); arr.clear (); // must be cleared } else break; while(!sta2.empty()){ tmp = sta2.top(); sta2.pop(); arr.push_back(tmp->val); if(tmp->right) sta1.push(tmp->right); if(tmp->left) sta1.push(tmp->left); } if(!arr.empty()){ res.push_back(arr); arr.clear (); // must be cleared } else break; } return res; } };