Skip to content

Latest commit

 

History

History
 
 

652. Find Duplicate Subtrees

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 

Given the root of 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 the same node values.

 

Example 1:

Input: root = [1,2,3,4,null,2,4,null,null,4]
Output: [[2,4],[4]]

Example 2:

Input: root = [2,1,1]
Output: [[1]]

Example 3:

Input: root = [2,2,2,3,null,3,null]
Output: [[2,3],[3]]

 

Constraints:

  • The number of the nodes in the tree will be in the range [1, 10^4]
  • -200 <= Node.val <= 200

Companies:
Google, Bloomberg

Related Topics:
Tree, Depth-First Search, Breadth-First Search, Binary Tree

Similar Questions:

Solution 1.

Must not encode null as empty string because then we can't tell if it's left child or right child.

// OJ: https://leetcode.com/problems/find-duplicate-subtrees/
// Author: github.com/lzl124631x
// 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
class Solution {
    unordered_map<string, int> cnt;
    vector<TreeNode*> ans;
    string dfs(TreeNode *node) {
        if (!node) return "#";
        auto s = to_string(node->val) + "," + dfs(node->left) + "," + dfs(node->right);
        if (++cnt[s] == 2) ans.push_back(node);
        return s;
    }
public:
    vector<TreeNode*> findDuplicateSubtrees(TreeNode* root) {
        dfs(root);
        return ans;
    }
};

Or this more generalized way. This generalized way also works for n-ary tree. For example, in 1948. Delete Duplicate Folders in System (Hard)

// OJ: https://leetcode.com/problems/find-duplicate-subtrees/
// Author: github.com/lzl124631x
// Time: O(N^2)
// Space: O(N^2)
class Solution {
    unordered_map<string, int> cnt;
    vector<TreeNode*> ans;
    string dfs(TreeNode *root) {
        if (!root) return "#";
        auto s = "(" + to_string(root->val) + dfs(root->left) + dfs(root->right) + ")";
        if (++cnt[s] == 2) ans.push_back(root);
        return s;
    }
public:
    vector<TreeNode*> findDuplicateSubtrees(TreeNode* root) {
        dfs(root);
        return ans;
    }
};