Binary Tree Right Side View
Function Name: rightSideView
Description
Given the root of a binary tree, imagine yourself standing on the right side of the tree, return the values of the nodes you can see ordered from top to bottom.
The binary tree's TreeNode is defined as: struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) {} };
The function should traverse the tree level by level and only take the farthest right node's value at each level.
The tree may contain duplicate values.
Requirements
- The function must take a single parameter, which is the root node of the binary tree.
- Do not use any helper libraries for tree manipulation; the traversal should be implemented manually.
- The function should return a list of integers representing the viewed values from the right side.
- If the tree is empty, the function should return an empty list.
Examples
If the function is given the following tree: 1 / \ 2 3 \ \ 5 4The function should return [1, 3, 4].The right side view of a tree that consists of a single node 1 should be [1].
Links
Sorting Rail Cars
Sun Dec 08 2024
Imagine a train sorting system where rail cars containing numbers need to be reordered according to their values. Each rail car is linked to the next, forming a chain. Your task is to write a function that receives a linked list representing a train of rail cars, and returns a linked list with the cars sorted in ascending order. You cannot use auxiliary arrays, pointers, or new data structures; the existing linked list must be manipulated to sort the cars.
Lights Out Puzzle
Tue Dec 10 2024
In the game 'Lights Out', there is a grid of lights that are either on (1) or off (0). When you toggle a light at position (i, j), it also toggles the lights vertically and horizontally adjacent to it. The task is to write a function that takes a 5x5 grid of the lights as an input and determines if the game is solvable. A game is considered solvable if there exists a sequence of moves that turns all the lights off.