#P17013. [GESP202606 六级] 满二叉树
[GESP202606 六级] 满二叉树
Problem Description
Given a rooted binary tree with nodes, the nodes are numbered in order as , and the root node is numbered .
For node , let the index of its left child be , and the index of its right child be . In particular, if the left child does not exist then , and if the right child does not exist then .
Each node in the tree corresponds to a subtree rooted at that node. You need to find, among all subtrees of the given rooted tree, how many of them are full binary trees.
A full binary tree means a binary tree where all leaves have the same depth, and every non-leaf node has exactly two children. For example, the following three binary trees are all full binary trees:
() () ()
/ \ / \
() () () ()
/ \ / \
() ()() ()
In the above binary tree with nodes, there are subtrees that are full binary trees (including the whole tree itself and all single leaf nodes).
Another example:
(1)
/ \
(2) (3)
/ \
(4) (5)
In the above binary tree with nodes, there are subtrees that are full binary trees (including the subtree rooted at node , and all single leaf nodes).
Input Format
The first line contains a positive integer , representing the number of nodes in the rooted binary tree.
The next lines each contain two non-negative integers , representing the indices of the left child and right child of node . The integers are separated by spaces.
Output Format
Output one line containing one integer, representing the number of full binary trees among all subtrees.
4
2 3
4 0
0 0
0 0
2
3
2 3
0 0
0 0
3
Hint
Constraints
For of the test points, it is guaranteed that .
For all test points, it is guaranteed that .
Translated by ChatGPT 5