#P12141. Determine Node Color in a Red-Black Tree
Determine Node Color in a Red-Black Tree
Determine Node Color in a Red-Black Tree
Little Blue recently learned about red-black trees, a special kind of binary tree where each node is either red or black. In the tree he constructed in his mind, the construction rules are as follows:
- The root node is red.
- If the current node \(\text{curNode}\) is red, then its left child \(\text{curNode.left}\) is red and its right child \(\text{curNode.right}\) is black.
- If the current node \(\text{curNode}\) is black, then its left child \(\text{curNode.left}\) is black and its right child \(\text{curNode.right}\) is red.
You are given a description of a path from the root in the tree. The path is represented as a string consisting of the characters 'L' and 'R', where each character indicates a move to the left or right child respectively. Your task is to determine whether the node at the end of this path is red or black.
Note: The tree is conceptually infinite. Even if the path is empty, it refers to the root node.
The color change rules can be formally written in \(\LaTeX\) as follows:
[ \begin{cases} \text{Root color} = \text{red}, \ \text{If } \text{curNode} = \text{red}: \quad \text{curNode.left} = \text{red},\quad \text{curNode.right} = \text{black}, \ \text{If } \text{curNode} = \text{black}: \quad \text{curNode.left} = \text{black},\quad \text{curNode.right} = \text{red}. \end{cases} ]
inputFormat
The input consists of a single line containing a string of characters 'L' and 'R' representing the path from the root. An empty string indicates the root node.
outputFormat
Output a single character: 'R' if the node is red, or 'B' if the node is black.
sample
L
R