[leetcode-652-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   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.

思路:

将每一个节点的左子节点的值和右结点的值都存储下来,组成一个字符串,作为索引,将对应节点保存到map里。

string serialize(TreeNode* root,unordered_map<string,vector<TreeNode*> >& mp)
{
  if(root==nullptr) return "";
  string s= "(" + serialize(root->left,mp) + to_string(root->val) + serialize(root->right,mp) + ")";
  mp[s].push_back(root);
  return s;
}
vector<TreeNode*> findDuplicateSubtrees(TreeNode* root)
{
    unordered_map<string,vector<TreeNode*> >mp;
    vector<TreeNode*>ret;
    serialize(root,mp);
    for(auto it = mp.begin();it != mp.end();it++)
    {
      if(it->second.size() > 1) ret.push_back(it->second[0]);
    }
    return ret;
} 

参考:

https://discuss.leetcode.com/topic/97601/c-clean-code

时间: 2024-08-05 18:47:50

[leetcode-652-Find Duplicate Subtrees]的相关文章

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

核心思想是:序列化树 序列化后,用String可以唯一的代表一棵树,其实就是前序遍历改造一下(空节点用符号表示): 一边序列化,一边用哈希表记录有没有重复的,如果有就添加,注意不能重复添加. 重点就是序列化树,序列化得到的String可以唯一的代表一棵树,这个思想很多题都用到了 并不是只是前序遍历就能唯一表示一棵树,加上结构信息就可以了. Map<String,TreeNode> map = new HashMap<>(); List<TreeNode> res = n

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] 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: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

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][Java]Contains Duplicate III

Contains Duplicate III Given an array of integers, find out whether there are two distinct indices i and j in the array such that the difference between nums[i] and nums[j] is at most t and the difference between i and j is at most k. 数组中是否存在两个元素,他们的

[LeetCode][JavaScript]Remove Duplicate Letters

Remove Duplicate Letters Given a string which contains only lowercase letters, remove duplicate letters so that every letter appear once and only once. You must make sure your result is the smallest in lexicographical order among all possible results

leetcode Find the Duplicate Number

题目连接 https://leetcode.com/problems/find-the-duplicate-number/ Find the Duplicate Number Description Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), prove that at least one duplicate number must exist.