Skip to content

BFS-2 Done#1578

Open
vaishnavi2231 wants to merge 1 commit intosuper30admin:masterfrom
vaishnavi2231:master
Open

BFS-2 Done#1578
vaishnavi2231 wants to merge 1 commit intosuper30admin:masterfrom
vaishnavi2231:master

Conversation

@vaishnavi2231
Copy link
Copy Markdown

No description provided.

@super30admin
Copy link
Copy Markdown
Owner

Your solutions for the right side view problem are excellent. Both BFS and DFS approaches are correctly implemented and efficient. Your code is clean and well-commented, which makes it easy to understand.

Strengths:

  • You provided two different approaches, showing a good understanding of both BFS and DFS.
  • The code is well-documented with comments explaining the approach and time/space complexity.
  • The variable names are clear and appropriate.

Areas for Improvement:

  • When submitting a solution for a specific problem, ensure that you only include code relevant to that problem. The inclusion of the "cousins-in-binary-tree" code in this file is unnecessary and could cause confusion. It's better to separate solutions for different problems into different files.
  • In the DFS solution for right side view, the traversal is preorder (root, left, right). However, to get the right side view, we typically want to traverse right first to ensure that we capture the rightmost node at each level without having to overwrite. But in your DFS, you are overwriting the value at each level with the current node value. Since you traverse left first and then right, the rightmost node will be the last one to update the value at that level, which is correct. However, note that this approach might be less intuitive than a right-first DFS. Alternatively, you could traverse right first and only add a value if the level hasn't been visited yet. For example:
    if level == len(result):
    result.append(root.val)
    then traverse right first and then left. This would avoid the overwriting and might be more efficient as you wouldn't need to update nodes that are not the rightmost. But your current solution is correct.

Overall, your solutions are correct and efficient. Well done!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants