All Downloads are FREE. Search and download functionalities are using the official Maven repository.

g0601_0700.s0652_find_duplicate_subtrees.Solution Maven / Gradle / Ivy

There is a newer version: 1.38
Show newest version
package g0601_0700.s0652_find_duplicate_subtrees;

// #Medium #Hash_Table #Depth_First_Search #Tree #Binary_Tree
// #2022_03_21_Time_32_ms_(60.16%)_Space_55.4_MB_(34.89%)

import com_github_leetcode.TreeNode;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
/**
 * 652 - Find Duplicate Subtrees\.
 *
 * Medium
 *
 * 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:**
 *
 * ![](https://assets.leetcode.com/uploads/2020/08/16/e1.jpg)
 *
 * **Input:** root = [1,2,3,4,null,2,4,null,null,4]
 *
 * **Output:** [[2,4],[4]]
 *
 * **Example 2:**
 *
 * ![](https://assets.leetcode.com/uploads/2020/08/16/e2.jpg)
 *
 * **Input:** root = [2,1,1]
 *
 * **Output:** [[1]]
 *
 * **Example 3:**
 *
 * ![](https://assets.leetcode.com/uploads/2020/08/16/e33.jpg)
 *
 * **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`
**/
public class Solution {
    public List findDuplicateSubtrees(TreeNode root) {
        Map map = new HashMap<>();
        List list = new ArrayList<>();
        helper(root, map, list);
        return list;
    }

    private String helper(TreeNode root, Map map, List list) {
        if (root == null) {
            return "#";
        }
        String key =
                helper(root.left, map, list) + "#" + helper(root.right, map, list) + "#" + root.val;
        map.put(key, map.getOrDefault(key, 0) + 1);
        if (map.get(key) == 2) {
            list.add(root);
        }
        return key;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy