# Part1:回顾

## 1.树的定义

• 节点的度：节点的子树个数
• 树的度：树的所有节点中最大的度数
• 叶节点：度为0的节点
• 根节点：没有父节点的节点
• 父节点：有子树的节点是其子树的根节点的父节点
• 子节点：若A是B的父节点，那么B是A的子节点
• 兄弟节点：具有同一父节点的各个节点彼此是兄弟节点

## 2.二叉树

• 第i层的最大节点数为：$2^{i-1}$。
• 这个很好理解解，每一层最多是上一层的2倍
• 深度为k的二叉树有最大节点总数为:$2^{k}-1$。
• 这是因为第i层最多有$2^{i-1}$个，一共有k层，所以最多有$2^{0}+2^{1}+…+2^{k-1}=2^{k}-1$个节点
• 对任何非空二叉树，$n_{i}$表示度为$i$的节点数量($i=0,1,2$)。那么满足$n_{0}=n_{2}+1$

• 利用两种方式来计算边的数量。由树的定义只除了根节点为，每个节点有且只有一个父节点，所以边的数量为$n_{0}+n_{1}+n_{2}-1$。再来换一种思路，度为$i$的节点数量为$n_{i}$，所以边的数量为$0\times n_{0}+1\times n_{1}+2\times n_{2}$
• 综上，$n_{0}+n_{1}+n_{2}-1=0\times n_{0}+1\times n_{1}+2\times n_{2}$，即$n_{0}=n_{2}+1$
• 最后一个结论还可以推广为k叉树，$n_{i}$表示度为$i$的节点数量($i=0,1,2…k$)。那么$n_{0}=\sum_{i=1}^{k}(i-1)n_{i}+1$。思路和之前的证明一样。

# Part2:作业

• 作业1

03-树1 树的同构（25 分）
给定两棵树T1和T2。如果T1可以通过若干次左右孩子互换就变成T2，则我们称两棵树是“同构”的。例如图1给出的两棵树就是同构的，因为我们把其中一棵树的结点A、B、G的左右孩子互换后，就得到另外一棵树。而图2就不是同构的。

图1

图2
现给定两棵树，请你判断它们是否是同构的。
输入格式:
输入给出2棵二叉树树的信息。对于每棵树，首先在一行中给出一个非负整数N (≤10)，即该树的结点数（此时假设结点从0到N−1编号）；随后N行，第i行对应编号第i个结点，给出该结点中存储的1个英文大写字母、其左孩子结点的编号、右孩子结点的编号。如果孩子结点为空，则在相应位置上给出“-”。给出的数据间用一个空格分隔。注意：题目保证每个结点中存储的字母是不同的。
输出格式:
如果两棵树是同构的，输出“Yes”，否则输出“No”。
输入样例1（对应图1）：
8
A 1 2
B 3 4
C 5 -
D - -
E 6 -
G 7 -
F - -
H - -
8
G - 4
B 7 6
F - -
A 5 1
H - -
C 0 -
D - -
E 2 -
输出样例1:
Yes
输入样例2（对应图2）：
8
B 5 7
F - -
A 0 3
C 6 -
H - -
D - -
G 4 -
E 1 -
8
D 6 -
B 5 -
E - -
H - -
C 0 2
G - 3
F - -
A 1 4
输出样例2:
No

• 作业2

03-树2 List Leaves（25 分）
Given a tree, you are supposed to list all the leaves in the order of top down, and left to right.
Input Specification:
Each input file contains one test case. For each case, the first line gives a positive integer N (≤10) which is the total number of nodes in the tree — and hence the nodes are numbered from 0 to N−1. Then N lines follow, each corresponds to a node, and gives the indices of the left and right children of the node. If the child does not exist, a “-“ will be put at the position. Any pair of children are separated by a space.
Output Specification:
For each test case, print in one line all the leaves’ indices in the order of top down, and left to right. There must be exactly one space between any adjacent numbers, and no extra space at the end of the line.

• 作业3

03-树3 Tree Traversals Again（25 分）
An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For example, suppose that when a 6-node binary tree (with the keys numbered from 1 to 6) is traversed, the stack operations are: push(1); push(2); push(3); pop(); pop(); push(4); pop(); pop(); push(5); push(6); pop(); pop(). Then a unique binary tree (shown in Figure 1) can be generated from this sequence of operations. Your task is to give the postorder traversal sequence of this tree.

Figure 1
Input Specification:
Each input file contains one test case. For each case, the first line contains a positive integer N (≤30) which is the total number of nodes in a tree (and hence the nodes are numbered from 1 to N). Then 2N lines follow, each describes a stack operation in the format: “Push X” where X is the index of the node being pushed onto the stack; or “Pop” meaning to pop one node from the stack.
Output Specification:
For each test case, print the postorder traversal sequence of the corresponding tree in one line. A solution is guaranteed to exist. All the numbers must be separated by exactly one space, and there must be no extra space at the end of the line.
Sample Input:
6
Push 1
Push 2
Push 3
Pop
Pop
Push 4
Pop
Pop
Push 5
Push 6
Pop
Pop
Sample Output:
3 4 2 6 5 1