Leetcode: Binary_Tree_Level_Order_Traversal_II, leetcode
I. Question
Given a binary tree, return the value of its node through hierarchical traversal from the bottom up. (That is, from left to right, layer by layer from the leaves to the root directory ).
Example: 3 output: [15, 7],
/\ [9, 20]
9 20 [3]
/\]
15 7
Ii. Analysis
When you see the question, you first think of layered traversal. Each layer is saved from left to right to a queue, and the element values are saved to a one-dimensional array in sequence, after traversing a layer, the result is saved to a two-dimensional array, which can be divided into the following steps:
1. Save the root node to the queue and count = 1;
2. traverse this layer (the last layer has been saved to the elements in the queue) and save the value to a one-dimensional array, and determine whether each node has left or right subnodes, if yes, the value of nextcount is added to the queue until the traversal is complete.
3. Save the one-dimensional array of the layer to the two-dimensional array, and use count = nextcount for the next time. If the queue is not empty at this time, (2)
4. When the entire binary tree traversal ends, the two-dimensional array is reversed and the result is obtained.
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public: vector<vector<int> > levelOrderBottom(TreeNode *root) { vector<vector<int>> ans;if(root==NULL) return ans;vector<int> eac;queue<TreeNode *> que;que.push(root);int count = 1;while(!que.empty()){eac.clear();int nextcount = 0;for(int i=0;i<count;i++){TreeNode *temp = que.front();que.pop();eac.push_back(temp->val);if(temp->left){nextcount++;que.push(temp->left);} if(temp->right){nextcount++;que.push(temp->right);} }count = nextcount;ans.push_back(eac);} reverse(ans.begin(),ans.end());return ans; }};
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public: vector<vector<int> > levelOrderBottom(TreeNode *root) { // Start typing your C/C++ solution below // DO NOT write int main() function vector<vector<int>> ans; if(root == NULL) return ans; vector<int> eac; queue<TreeNode *> que; queue<TreeNode *> que2; // extra space que.push(root); while(!que.empty()){ TreeNode *temp = que.front(); que.pop(); if(temp != NULL){ eac.push_back(temp->val); if(temp->left) que2.push(temp->left); if(temp->right) que2.push(temp->right); } if(que.empty()){ // one row end ans.push_back(eac); eac.clear(); swap(que, que2); } } reverse(ans.begin(), ans.end()); // reverse vector }};
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public: vector<vector<int> > levelOrderBottom(TreeNode *root) { // Start typing your C/C++ solution below // DO NOT write int main() function list<vector<int> > retTemp; queue<TreeNode *> trace; trace.push(root); trace.push(NULL); vector<int> curLevel; while(true) { TreeNode *cur = trace.front(); trace.pop(); if(cur) { curLevel.push_back(cur->val); if(cur->left) trace.push(cur->left); if(cur->right) trace.push(cur->right); } else { if(curLevel.size()) { retTemp.push_front(curLevel); curLevel.erase(curLevel.begin(),curLevel.end()); trace.push(NULL); } else { break; } } } vector<vector<int> > ret; for(list<vector<int> >::iterator it = retTemp.begin(); it != retTemp.end(); ++it) { ret.push_back(*it); } return ret; }};
C ++ leetcode always says that the compilation is wrong. The key is that I don't even have 77th lines, only those lines ......
Neither leetcode can define the main function.
You need to construct a Solution class
The main of the 77 rows of redefine happens to be the main function of leetcode used to test the Solution you write.
What is leetcode?
There are many programming and interview questions, which can be compiled and run online. It is difficult. If you can do it all by yourself, it is very helpful for large companies. I just did the questions there.