[LeetCode]652. Find Duplicate Subtrees找到重复树

核心思想是:序列化树

序列化后,用String可以唯一的代表一棵树,其实就是前序遍历改造一下(空节点用符号表示);

一边序列化,一边用哈希表记录有没有重复的,如果有就添加,注意不能重复添加。

重点就是序列化树,序列化得到的String可以唯一的代表一棵树,这个思想很多题都用到了

并不是只是前序遍历就能唯一表示一棵树,加上结构信息就可以了。

Map<String,TreeNode> map = new HashMap<>();
    List<TreeNode> res = new ArrayList<>();
    public List<TreeNode> findDuplicateSubtrees(TreeNode root) {
        serialize(root);
        return res;
    }
    public String serialize(TreeNode root)
    {
        StringBuilder s = new StringBuilder();
        if (root==null)
        {
            s.append("#");
            s.append(",");
            return new String(s);
        }
        s.append(root.val);
        s.append(",");
        s.append(serialize(root.left));
        s.append(serialize(root.right));
        String cur = new String(s);
        if (map.containsKey(cur))
        {
            TreeNode t = map.get(cur);
            if (!res.contains(t)) res.add(t);
        }
        else map.put(cur,root);
        return cur;
    }

原文地址:https://www.cnblogs.com/stAr-1/p/8378097.html

时间: 2024-11-13 06:54:14

[LeetCode]652. Find Duplicate Subtrees找到重复树的相关文章

[LeetCode] 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 oneof them. Two trees are duplicate if they have the same structure with same node values. Example 1: 1 / 2 3 / / 4

LeetCode 217. Contains Duplicate (包含重复项)

Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct. 题目标签:Array, Hash Table 题目给了我们一个nums arr

LeetCode 217 Contains Duplicate(包含重复数字)(Vector、hash)

翻译 给定一个整型数字数组,找出这个数组是否包含任何重复内容. 如果任何值出现了至少两次,那么返回真(true), 如果每个值都是互不相同的,那么返回假(false). 原文 Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the array, and it s

leetcode—217 Contains Duplicate(包含重复的数)

<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in t

LeetCode 196. Delete Duplicate Emails (删除重复的电子邮箱)

题目标签: 题目给了我们一个 email 的表格,让我们删除重复的. 建立Person p1,Person p2,当email 相同时,而且 p1 id 要大于 p2 id 时候,删除这一行. Java Solution: Runtime:  869 ms, faster than 33 % Memory Usage: N/A 完成日期:06/01/2019 关键点:p1.Id > p2.Id # Write your MySQL query statement below DELETE p1

652. Find Duplicate Subtrees

/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: unordered_map<string, vector<TreeNode*>> s; ve

Leetcode 652.寻找重复的子树

寻找重复的子树 给定一棵二叉树,返回所有重复的子树.对于同一类的重复子树,你只需要返回其中任意一棵的根结点即可. 两棵树重复是指它们具有相同的结构以及相同的结点值. 下面是两个重复的子树: 因此,你需要以列表的形式返回上述重复子树的根结点. 思路 Intuition We can serialize each subtree. For example, the tree 1 / 2 3 / 4 5 can be represented as the serialization 1,2,#,#,3

LeetCode:Contains Duplicate - 判断数组内是否有重复元素

1.题目名称 Contains Duplicate(判断数组内是否有重复元素) 2.题目地址 https://leetcode.com/problems/contains-duplicate/ 3.题目内容 英文:Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in

LeetCode:Contains Duplicate II - 判断数组内是否有重复元素2

1.题目名称 Contains Duplicate II(判断数组内是否有重复元素2) 2.题目地址 https://leetcode.com/problems/contains-duplicate-ii/ 3.题目内容 英文:Given an array of integers and an integer k, find out whether there are two distinct indices i and j in the array such that nums[i] = nu