SZ#G6BT13. 【GESP强化 六级】二叉树的信息

提交1 通过1
通过率100%
时间限制2000ms
内存限制256MiB

题目描述

给定一棵有根二叉树,节点编号为 11nn。每个节点最多有一个左孩子和一个右孩子,编号 00 表示相应孩子不存在。

对每个节点 uu,需要输出它的父节点、兄弟节点、孩子数量、深度、高度和节点类型。根节点的父节点和兄弟节点均记为 00;节点深度是根到该节点所经过的边数;节点高度是该节点到其子树中最深叶子所经过的边数。节点类型分为 rootinternal nodeleaf

输入格式

第一行一个整数 nn,表示节点数量。

接下来 nn 行,第 ii 行包含两个整数 li,ril_i,r_i,分别表示节点 ii 的左孩子和右孩子。编号 00 表示相应孩子不存在。输入保证这些数据恰好构成一棵有根二叉树。

输出格式

按照节点编号从 11nn 的顺序,每个节点输出一行,格式为 node i: parent = p, sibling = s, degree = d, depth = dep, height = h, type。其中 typerootinternal nodeleaf

1
0 0
node 1: parent = 0, sibling = 0, degree = 0, depth = 0, height = 0, root
2
2 0
0 0
node 1: parent = 0, sibling = 0, degree = 1, depth = 0, height = 1, root
node 2: parent = 1, sibling = 0, degree = 0, depth = 1, height = 0, leaf
3
2 3
0 0
0 0
node 1: parent = 0, sibling = 0, degree = 2, depth = 0, height = 1, root
node 2: parent = 1, sibling = 3, degree = 0, depth = 1, height = 0, leaf
node 3: parent = 1, sibling = 2, degree = 0, depth = 1, height = 0, leaf

数据范围与约定

  • 1n251\le n\le25
  • 输入恰好描述一棵合法二叉树。