#P15801. [GESP202603 六级] 完全二叉树

    ID: 17864 远端评测题 1000ms 512MiB 尝试: 7 已通过: 2 显示难度普及 上传者: 标签>树形数据结构树形 DP树的遍历2026GESP

[GESP202603 六级] 完全二叉树

Background

Related multiple-choice and true/false questions: https://ti.luogu.com.cn/problemset/1210.

Problem Description

Given a rooted binary tree with nn nodes, the nodes are numbered 1,2,,n1,2,\dots,n in order, and the root node is numbered 11.

For node ii, its left child is denoted as lil_i, and its right child is denoted as rir_i. In particular, if the left child does not exist then li=0l_i=0, and if the right child does not exist then ri=0r_i=0.

Each node in the tree corresponds to a subtree rooted at that node. Please find, among all nn subtrees of the given rooted tree, how many of them are complete binary trees.

Input Format

The first line contains a positive integer nn, representing the number of nodes in the rooted binary tree.

The next nn lines each contain two positive integers li,ril_i, r_i, representing the index of the left child and the index of the right child of node ii.

Output Format

Output one line containing one integer, representing the number of complete binary trees among all subtrees.

4
2 3
4 0
0 0
0 0
4
4
2 3
0 0
4 0
0 0
3

Hint

For 40%40\% of the test points, 1n5001\leq n\leq 500 is guaranteed.

For all test points, 1n1051\leq n\leq 10^5 is guaranteed.

Translated by ChatGPT 5