-
Notifications
You must be signed in to change notification settings - Fork 18
Solution #199 - Daniel/Edited - 17.03.2025 #38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
danzang100
wants to merge
3
commits into
Dijkstra-Edu:master
Choose a base branch
from
danzang100:feature/views-of-tree
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
16 changes: 16 additions & 0 deletions
16
Trees (Binary Trees)/Views of Trees/Left and Right View/Explanation.md
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
Left View of tree. | ||
|
||
Brute Force Approach: | ||
|
||
1. Perform a complete level order traversal of the tree and maintain a matrix of all nodes visited by level. | ||
2. Choose only the first element for each level and return. | ||
|
||
Time Complexity: O(n) | ||
Space Complexity: O(n) | ||
|
||
Optimal Approach: | ||
|
||
1. Perform DFS traversal of the tree prioritising the left subtree each time. | ||
2. Take the first element of each level and return. | ||
|
||
For right view, repeat the same but take the last element of the matrix or perform DFS with the right subtree prioritised. | ||
28 changes: 28 additions & 0 deletions
28
Trees (Binary Trees)/Views of Trees/Left and Right View/Solution.cpp
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
class Solution { | ||
public: | ||
vector<int> leftView(Node *root) { | ||
vector<int> result; | ||
traverse(root, 0, result); | ||
return result; | ||
} | ||
void traverse(Node* root, int level, vector<int>& res){ | ||
//BRUTE FORCE APPROACH | ||
if(root == NULL) return; | ||
if(res.size() <= level){ | ||
res.push_back({}); | ||
} | ||
res[level].push_back(root->data); | ||
traverse(root->left, level+1, res); | ||
traverse(root->right, level+1, res); | ||
|
||
//OPTIMAL APPROACH | ||
if(!root) return; | ||
|
||
if(res.size() == level){ | ||
res.push_back(root->data); | ||
} | ||
|
||
traverse(root->left, level+1, res); | ||
traverse(root->right, level+1, res); | ||
} | ||
}; |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can be much better! utilize Markdown and try to explain each step + Complexities.