Binary Tree Right Side View

Advanced

Imagine you have a magical tree, specifically a binary tree. This tree grows in such a way that every branch (or node) can have up to two children—one to the left, and one to the right. Now, visualize standing on the far right side of this magical tree and looking at it. You will only see some of the nodes since others might be obscured by those closer to you. Your task is to find out which nodes are visible from the right side of the tree.

Your Task:

  • Implement the function right_side_view(tree_dict) where tree_dict is a nested dictionary representing the binary tree.
  • Build the tree from the dictionary within your function.
  • Return a list of the node values that would be visible if you looked at the tree from the right side.

Example:

Imagine this tree:

   1
  / \
 2   3
  \   \
   5   4

The dictionary representation is:

{ "val": 1, "left": { "val": 2, "left": null, "right": {"val": 5, "left": null, "right": null} }, "right": { "val": 3, "left": null, "right": {"val": 4, "left": null, "right": null} } }

If you look from the right side, you'll see the nodes with values [1, 3, 4].

Function Signature:

def right_side_view(tree_dict: dict) -> List[int]:

Try solving the problem using breadth-first traversal or any other strategy that excites your logical thinking! Have fun!

Geek
Inputs

Arg 1:

{"val":1,"left":{"val":2,"left":null,"right":{"val":5,"left":null,"right":null}},"right":{"val":3,"left":null,"right":{"val":4,"left":null,"right":null}}}
Expected Output
[1, 3, 4]
Actual Output
Binary Tree Right Side View [advanced] | Coding for Kids