Implement right side view of binary tree#266
Merged
SjxSubham merged 1 commit intoSjxSubham:mainfrom Oct 29, 2025
Merged
Conversation
Reviewer's GuideThis PR adds a DFS-based recursive solution for the right side view of a binary tree, using a helper function that traverses the right subtree before the left while tracking depth to capture the first node seen at each level. Sequence diagram for rightSideView traversal (DFS right-first)sequenceDiagram
participant "Caller"
participant Solution
participant "TreeNode (root)"
Caller->>Solution: rightSideView(root)
Solution->>Solution: traverseRightSide(root, mainLevel, 0, ans)
alt root is not NULL
Solution->>Solution: traverseRightSide(root->right, mainLevel, level+1, ans)
Solution->>Solution: traverseRightSide(root->left, mainLevel, level+1, ans)
end
Solution-->>Caller: return ans
Class diagram for Solution and TreeNode classes (right side view implementation)classDiagram
class TreeNode {
int val
TreeNode* left
TreeNode* right
TreeNode()
TreeNode(int x)
TreeNode(int x, TreeNode* left, TreeNode* right)
}
class Solution {
vector<int> rightSideView(TreeNode* root)
void traverseRightSide(TreeNode* root, int &mainLevel, int level, vector<int>& ans)
}
TreeNode <|-- Solution: uses
File-Level Changes
Possibly linked issues
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Hey there - I've reviewed your changes - here's some feedback:
- Your PR description describes a BFS approach but you actually implemented a DFS solution – update the description to match your chosen strategy for clarity.
- Remove the unused commented‐out unordered_map and consider renaming ‘mainLevel’ to something like ‘maxDepth’ or ‘currentMaxLevel’ for better readability.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- Your PR description describes a BFS approach but you actually implemented a DFS solution – update the description to match your chosen strategy for clarity.
- Remove the unused commented‐out unordered_map and consider renaming ‘mainLevel’ to something like ‘maxDepth’ or ‘currentMaxLevel’ for better readability.Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
4 tasks
SjxSubham
approved these changes
Oct 29, 2025
🎉 Congrats on getting your PR merged in, @Subhosjx! 🙌🏼Thanks for your contribution every effort helps improve the project. Looking forward to seeing more from you! 🥳✨ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PR Title Format: Problem no.Problem name.cpp
Intuition
When viewing a binary tree from the right side, only the rightmost node at each depth is visible. That means for every level of the tree, we need to capture the last node in that level’s traversal.
Approach
We can solve this with BFS (level order traversal):
Code Solution (C++)
Related Issues
By submitting this PR, I confirm that:
Summary by Sourcery
New Features: