Let's make this one less mysterious. For 199. Binary Tree Right Side View, the solution in this repository is mainly a graph traversal 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: graph traversal, two pointers, sliding window.
Guide
When?
This is the kind of solution you want when the problem has structure hiding inside a messy-looking input. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are rightSideView, preorder.
Guide
Why?
The point of the implementation is not to make the code longer. It is to avoid doing the same thinking twice.
- 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:
- 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//Runtime: 8 ms, faster than 30.77% of C++ online submissions for Binary Tree Right Side View.
02//Memory Usage: 12 MB, less than 5.48% of C++ online submissions for Binary Tree Right Side View.
03/**
04 * Definition for a binary tree node.
05 * struct TreeNode {
06 * int val;
07 * TreeNode *left;
08 * TreeNode *right;
09 * TreeNode() : val(0), left(nullptr), right(nullptr) {}
10 * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
11 * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
12 * };
13 */
14class Solution {
15public:
16 vector<int> rightSideView(TreeNode* root) {
17 vector<int> ans;
18 if(!root) return ans;
19
20 queue<TreeNode*> q;
21
22 q.push(root);
23
24 while(!q.empty()){
25 int level_size = q.size();
26 int level_last;
27
28 while(level_size--){
29 TreeNode* cur = q.front(); q.pop();
30
31 level_last = cur->val;
32
33 if(cur->left) q.push(cur->left);
34 if(cur->right) q.push(cur->right);
35 }
36
37 ans.push_back(level_last);
38 }
39
40 return ans;
41 }
42};
43
44//dfs, preorder
45//Runtime: 4 ms, faster than 79.52% of C++ online submissions for Binary Tree Right Side View.
46//Memory Usage: 12.2 MB, less than 5.48% of C++ online submissions for Binary Tree Right Side View.
47class Solution {
48public:
49 vector<int> ans;
50
51 void preorder(TreeNode* node, int depth){
52 if(!node) return;
53
54 /*
55 only push value to the vector
56 when we meet first element in that depth
57
58 note that right child is visited before left child,
59 so the first element in that depth will be
60 the rightmost node in that depth
61 */
62 if(depth == ans.size()){
63 ans.push_back(node->val);
64 }
65
66 preorder(node->right, depth+1);
67 preorder(node->left, depth+1);
68 }
69
70 vector<int> rightSideView(TreeNode* root) {
71 preorder(root, 0);
72
73 return ans;
74 }
75};
Cost