题目描述
给定一棵有根二叉树,节点编号为 到 。每个节点最多有一个左孩子和一个右孩子,编号 表示相应孩子不存在。
对每个节点 ,需要输出它的父节点、兄弟节点、孩子数量、深度、高度和节点类型。根节点的父节点和兄弟节点均记为 ;节点深度是根到该节点所经过的边数;节点高度是该节点到其子树中最深叶子所经过的边数。节点类型分为 root、internal node 和 leaf。
输入格式
第一行一个整数 ,表示节点数量。
接下来 行,第 行包含两个整数 ,分别表示节点 的左孩子和右孩子。编号 表示相应孩子不存在。输入保证这些数据恰好构成一棵有根二叉树。
输出格式
按照节点编号从 到 的顺序,每个节点输出一行,格式为 node i: parent = p, sibling = s, degree = d, depth = dep, height = h, type。其中 type 为 root、internal node 或 leaf。
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
数据范围与约定
- 输入恰好描述一棵合法二叉树。