描述
Given a binary tree, return all duplicate subtrees. For each kind of duplicate subtrees, you only need to return the root node of any oneof them.
Two trees are duplicate if they have the same structure with same node values.
Example 1:
1 | 1 |
The following are two duplicate subtrees:
1 | 2 |
and
1 | 4 |
Therefore, you need to return above trees’ root in the form of a list.
分析
先序遍历,把路径序列化为字符串作为 map 的 key。
解决方案1(Java)
1 | /** |
相关问题
- (H) Serialize and Deserialize Binary Tree
- (M) Serialize and Deserialize BST
- (E) Construct String from Binary Tree