# 这些树是同构的吗？

21

## 介绍

``````  0       0
/|\     /|\
1 3 4   1 2 5
|\       /|
2 5     3 4
``````

``````[0,0,1,3,2,2,5,0]
1 2 3 4 5 6 7 8
``````

``````  0
/|\
1 2 8
| |\
3 5 6
| |
4 7
``````

## 输入项

1. 他们不是空的。
2. 它们具有相同的长度。
3. 每个列表都`L`满足`L[i] < i`所有（基于1的）索引`i`

## 测试用例

``````[0] [0]
[0,1,2,1] [0,1,1,3]
[0,1,1,3,3] [0,1,2,2,1]
[0,1,0,1,2,3,3,0] [0,0,2,1,0,4,2,1]
[0,1,2,3,1,2,3,0,8] [0,1,0,3,3,4,4,7,7]
``````

``````[0,0] [0,1]
[0,1,2,0,3,3,4] [0,1,2,3,0,4,3]
[0,1,0,1,2,3,3,0] [0,0,2,1,0,5,2,1]
[0,1,1,0,1,3,2,1,5] [0,1,0,3,3,3,2,5,2]
[0,1,2,3,1,2,3,0,8] [0,1,0,1,4,4,5,6,6]
[0,1,0,2,0,3,0,4,0,5] [0,0,2,1,0,3,4,0,0,9]
``````

@DigitalTrauma Dangit，您使OP不允许内置函数...我有一个60字节的Mma解决方案...
LegionMammal978 2015年

2

# Mathematica，48个字节

``````SameQ@@(Sort//@(0(0//.PositionIndex@#))&/@{##})&
``````

``````IsomorphicGraphQ@@(Graph@*MapIndexed[#->Tr@#2&]/@{##})&
``````

6

# Python，83岁

``````f=lambda l,i=0:sorted(f(l,j+1)for j,e in enumerate(l)if e==i)
lambda a,b:f(a)==f(b)
``````

`f`返回子树的规范化形式，该子树是其规范化子级的排序列表。然后，我们必须简单地检查每棵树的规范化形式是否相等。