Let's make this one less mysterious. For 107. Binary Tree Level Order Traversal II, the solution in this repository is mainly a two pointers solution.
Guide
What?
Before optimizing anything, pin down what information is still useful after each move. Instead of trying to be clever immediately, read the code as a sequence of questions:
- What state are we keeping?
- How do we move from one state to the next?
- When do we know the answer is already determined?
For this file, the main tools are: two pointers, sliding window.
Guide
When?
Reach for this shape when a direct simulation would work logically but waste time revisiting the same information. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The solution is organized around the main LeetCode entry point and a few local helpers.
Guide
Why?
The solution works because it narrows the problem until every update has a clear reason to exist.
- The queue gives the solution a level-by-level or frontier-style traversal.
- The two-dimensional vector is the memory of the solution: grid state, DP state, or adjacency shape.
- The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.
Guide
How?
Walk through the solution in this order:
- Initialize the memory or helper structure.
- Process candidates in the order the invariant expects.
- Update the answer only when the current state is valid.
- Return the value that represents the fully processed input.
The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.
Guide
Complexity
- Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
- Space: O(n) in the usual case for auxiliary containers or recursion
Guide
C++ Solution
Your submission
The accepted solution
01/**
02Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root).
03
04For example:
05Given binary tree [3,9,20,null,null,15,7],
06 3
07 / \
08 9 20
09 / \
10 15 7
11return its bottom-up level order traversal as:
12[
13 [15,7],
14 [9,20],
15 [3]
16]
17**/
18
19//Runtime: 8 ms, faster than 100.00% of C++ online submissions for Binary Tree Level Order Traversal II.
20//Memory Usage: 13.8 MB, less than 82.65% of C++ online submissions for Binary Tree Level Order Traversal II.
21
22/**
23 * Definition for a binary tree node.
24 * struct TreeNode {
25 * int val;
26 * TreeNode *left;
27 * TreeNode *right;
28 * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
29 * };
30 */
31class Solution {
32public:
33 vector<vector<int>> levelOrderBottom(TreeNode* root) {
34 if(root == NULL) return vector<vector<int>>();
35 queue<TreeNode*> q;
36 TreeNode* cur;
37 vector<int> level;
38 vector<vector<int>> ans;
39 int levelCount = 0;
40
41 q.push(root);
42 levelCount++;
43
44 while(!q.empty()){
45 cur = q.front();
46 q.pop();
47 levelCount--;
48
49 level.push_back(cur->val);
50
51 if(cur->left) q.push(cur->left);
52 if(cur->right) q.push(cur->right);
53
54 //cur is the last of this level
55 if(levelCount == 0){
56 ans.push_back(level);
57 level = vector<int>();
58 //now we have push this node and its sibling's children to queue
59 levelCount = q.size();
60 }
61 }
62 reverse(ans.begin(), ans.end());
63 return ans;
64 }
65};
66
67//another style
68//Runtime: 32 ms, faster than 10.47% of C++ online submissions for Binary Tree Level Order Traversal II.
69//Memory Usage: 13.1 MB, less than 25.93% of C++ online submissions for Binary Tree Level Order Traversal II.
70/**
71 * Definition for a binary tree node.
72 * struct TreeNode {
73 * int val;
74 * TreeNode *left;
75 * TreeNode *right;
76 * TreeNode() : val(0), left(nullptr), right(nullptr) {}
77 * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
78 * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
79 * };
80 */
81class Solution {
82public:
83 vector<vector<int>> levelOrderBottom(TreeNode* root) {
84 vector<vector<int>> ans;
85
86 if(root != nullptr){
87 TreeNode* cur;
88 queue<TreeNode*> q;
89 q.push(root);
90
91 while(!q.empty()){
92 int levelSize = q.size();
93 vector<int> level;
94 while(levelSize-- > 0){
95 cur = q.front(); q.pop();
96
97 level.push_back(cur->val);
98
99 if(cur->left) q.push(cur->left);
100 if(cur->right) q.push(cur->right);
101 }
102 ans.insert(ans.begin(), level);
103 }
104 }
105
106 return ans;
107 }
108};
Cost