348. Nodes With Exactly One Child
Print how many nodes have exactly one child, on either side.
The condition is an EXCLUSIVE or: one child present and the other absent. A node with two children does not count and neither does a leaf.
Together with the previous two problems this partitions the tree: every node is a
leaf, has one child, or has two, and the three counts add up to n. That is a
good check on all three answers.
Constraints - `1 ≤ n ≤ 100000` - `-1000000000 ≤ value ≤ 1000000000` - The input always forms a valid tree rooted at node 1.
Input
The first line contains an integer n, the number of nodes.
The second line contains n values, where node i holds the ith value.
Each of the next n lines contains two integers, the left and right child of
node i, using 0 for no child. Node 1 is the root.
Output
Print one integer, the number of nodes with exactly one child.