# Find Duplicate Subtrees

Given a binary tree, return all duplicate subtrees. For each kind of duplicate subtrees, you only need to return the root node of any **one** of them.

Two trees are duplicate if they have the same structure with same node values.

**Example 1:**

```
        1
       / \
      2   3
     /   / \
    4   2   4
       /
      4
```

The following are two duplicate subtrees:

```
      2
     /
    4
```

and

```
    4
```

Therefore, you need to return above trees' root in the form of a list.

Here is a solution.

```
class Solution {
    private final List<TreeNode> duplicates = new ArrayList<>();

    public List<TreeNode> findDuplicateSubtrees(TreeNode root) {
        // a node in a binary tree is: node, node.left and node.right
        // we need to iterate through the nodes and build key for each node, then as we are building the keys and a hash map, we check for duplicate keys, if duplicate key is found, we add that node into list
        // 1 -> 2 ->null,null
        // 1 -> 2 ->null,null
        find(root);
        return duplicates;
    }

    private Map<String, Integer> keys = new HashMap<>();

    public String find(TreeNode node) {
        if (node == null) return "-";
        String key = String.valueOf(node.val) + find(node.left) + find(node.right);
        System.out.println(key);
        if (keys.containsKey(key) && keys.get(key) == 0) {
            System.out.println("adding...");
            duplicates.add(node);
            keys.put(key, 1);
        } else {
            if (!keys.containsKey(key)) {
                keys.put(key, 0);
            }
        }
        return key;
    }
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://ondrej-kvasnovsky-2.gitbook.io/algorithms/data-structures/map/find-duplicate-subtrees.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
