MCTS how to prevent Selecting a TERMINAL state in TRAVERSAL Phase?
What you can do is to add a counter $t$ to each node, initially 0, indicating how many of its children are terminal.
When $t = c$ where $c$ is the number of children of a node, the node itself becomes terminal and increments the $t$ counter of its parent. Note that with this definition leaf nodes are automatically terminal, as they have no children, giving $0 = 0$.
As one more optimization, if a node has a terminal child that is losing, you can immediately mark its parent as terminal and winning.
Then, during selection you can ignore any terminal child.