Skip to content

Completed BFS-2#1587

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

Completed BFS-2#1587
viveksharma180 wants to merge 1 commit intosuper30admin:masterfrom
viveksharma180:master

Conversation

@viveksharma180
Copy link
Copy Markdown

No description provided.

@super30admin
Copy link
Copy Markdown
Owner

Evaluation completed, but no feedback text was generated.

@super30admin
Copy link
Copy Markdown
Owner

Right Side View of a tree (BinaryRightSide.py)

Evaluation completed, but no feedback text was generated.

VERDICT: NEEDS_IMPROVEMENT


Cousins in Tree (CousinsBinary.py)

Your solutions are well-written and cover multiple approaches (BFS with parent tracking, BFS with sibling check, and DFS). This shows a good understanding of the problem.

Strengths:

  • You provided three different solutions, which demonstrates flexibility.
  • The code is clean and well-commented.
  • You included time and space complexity analysis.

Areas for improvement:

  1. In BFSSolution2, while the sibling check is correct, it might be less intuitive. Consider adding a comment to explain that we are checking for siblings at the parent level to avoid having to store parents for all nodes.
  2. In the DFS solution, using instance variables is acceptable, but be cautious in a real interview setting because multiple calls to the same method might interfere. However, since the problem is standalone, it is fine. Alternatively, you could use a helper function that returns the depth and parent for a node, but that would require traversing the tree twice. So your approach is efficient.
  3. In BFSSolution2, you have a condition that continues after finding x or y. This is good because it avoids unnecessary checks. However, you could also break early if both are found at the same level and not siblings, but your current structure checks at the end of the level, which is efficient.

Overall, your solutions are correct and efficient.

VERDICT: PASS

@super30admin
Copy link
Copy Markdown
Owner

Strengths:

  • You have provided multiple solutions for each problem, showing a good understanding of different approaches.
  • Your code is well-commented and structured, making it easy to follow.
  • You have correctly analyzed the time and space complexities for each solution.

Areas for Improvement:

  • For the DFS solution of the right side view, the current approach of overwriting the value at each depth during an in-order traversal (root, left, right) does not guarantee that the rightmost node is captured. Instead, you should traverse the right subtree first (root, right, left) and only add the first node you encounter at each depth. This way, the first node at each depth in the DFS (which would be the rightmost) is added to the result list, and you don't need to overwrite. Alternatively, you can check if the current depth is equal to the size of the result list and then add the node value (which would be the first time reaching that depth), but only if you traverse right first. Here's a corrected DFS approach:

    def rightSideView(self, root: Optional[TreeNode]) -> List[int]:
        self.result = []
        self.dfs(root, 0)
        return self.result
    
    def dfs(self, node, depth):
        if not node:
            return
        if depth == len(self.result):
            self.result.append(node.val)
        self.dfs(node.right, depth+1)
        self.dfs(node.left, depth+1)
    
  • In the second BFS solution for cousins (BFSSolution2), the check for siblings is done only when both left and right children are not null. This is correct because siblings must share the same parent and be at the same level. However, note that the code continues to process even after finding x or y, which is acceptable. But ensure that the sibling check is comprehensive.

Overall, your solutions are mostly correct, but the DFS for right side view needs correction.

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