Formatted question description: https://leetcode.ca/all/652.html

652. Find Duplicate Subtrees (Medium)

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.

Companies:
Uber, Amazon, Google, Microsoft

Related Topics:
Tree

Similar Questions:

Solution 1.

// OJ: https://leetcode.com/problems/find-duplicate-subtrees/

// Time: O(N^2) since string concatenation takes O(N) time on average
// Space: O(N^2) since string take O(N) space on average
// Ref: https://leetcode.com/problems/find-duplicate-subtrees/solution/
class Solution {
private:
    unordered_map<string, int> treeToCnt;
    vector<TreeNode*> ans;
    string collect(TreeNode *node) {
        if (!node) return "#";
        string s = to_string(node->val) + "," + collect(node->left) + "," + collect(node->right);
        if (treeToCnt[s] == 1) ans.push_back(node);
        treeToCnt[s]++;
        return s;
    }
public:
    vector<TreeNode*> findDuplicateSubtrees(TreeNode* root) {
        collect(root);
        return ans;
    }
};

Java

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public List<TreeNode> findDuplicateSubtrees(TreeNode root) {
        List<TreeNode> duplicateList = new ArrayList<TreeNode>();
        if (root == null)
            return duplicateList;
        Set<String> visitedSet = new HashSet<String>();
        Set<String> duplicateSet = new HashSet<String>();
        Queue<TreeNode> queue = new LinkedList<TreeNode>();
        queue.offer(root);
        while (!queue.isEmpty()) {
            TreeNode node = queue.poll();
            String str = treeToString(node);
            if (!visitedSet.add(str) && duplicateSet.add(str))
                duplicateList.add(node);
            TreeNode left = node.left, right = node.right;
            if (left != null)
                queue.offer(left);
            if (right != null)
                queue.offer(right);
        }
        return duplicateList;
    }

    public String treeToString(TreeNode root) {
        List<String> list = new ArrayList<String>();
        list.add(String.valueOf(root.val));
        Queue<TreeNode> queue = new LinkedList<TreeNode>();
        queue.offer(root);
        while (!queue.isEmpty()) {
            TreeNode node = queue.poll();
            TreeNode left = node.left, right = node.right;
            if (left != null) {
                list.add(String.valueOf(left.val));
                queue.offer(left);
            } else
                list.add("null");
            if (right != null) {
                list.add(String.valueOf(right.val));
                queue.offer(right);
            } else
                list.add("null");
        }
        return list.toString();
    }
}

All Problems

All Solutions