The trick here is to name the state correctly, then let the implementation follow. For 662. Maximum Width of Binary Tree, the solution in this repository is mainly a two pointers solution.
Guide
What?
We want to turn the problem statement into a smaller set of decisions the computer can repeat safely. 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.
The notes already sitting in the source point us in the right direction:
- bfs, level-wise
Guide
When?
This pattern shows up when the brute force version has too many repeated checks, too many possible branches, or too much bookkeeping to do by hand. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are widthOfBinaryTree.
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 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:
- Read the setup variables first.
- Follow the main loop or recursive helper next.
- Watch where invalid states get skipped.
- Check which value survives to the return statement.
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//bfs, level-wise
02//Runtime: 4 ms, faster than 97.63% of C++ online submissions for Maximum Width of Binary Tree.
03//Memory Usage: 15.7 MB, less than 83.60% of C++ online submissions for Maximum Width of Binary Tree.
04/**
05 * Definition for a binary tree node.
06 * struct TreeNode {
07 * int val;
08 * TreeNode *left;
09 * TreeNode *right;
10 * TreeNode() : val(0), left(nullptr), right(nullptr) {}
11 * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
12 * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
13 * };
14 */
15class Solution {
16public:
17 int widthOfBinaryTree(TreeNode* root) {
18 if(root == nullptr) return 0;
19
20 int ans = 0;
21
22 queue<pair<TreeNode*, int>> q;
23
24 q.push({root, 0});
25
26 while(!q.empty()){
27 int levelSize = q.size();
28
29 int firstId = q.front().second;
30 int id = firstId;
31
32 while(levelSize-- > 0){
33 pair<TreeNode*, int> curP = q.front(); q.pop();
34 TreeNode* cur = curP.first;
35 id = curP.second;
36
37 if(cur->left){
38 // - firstId*2: to avoid overflow
39 q.push({cur->left, id*2 - firstId*2});
40 }
41
42 if(cur->right){
43 q.push({cur->right, id*2 + 1 - firstId*2});
44 }
45 }
46
47 ans = max(ans, id - firstId + 1);
48 }
49
50 return ans;
51 }
52};
Cost