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,4,#,#,5,#,#, which is a unique representation of the tree.

Algorithm

Perform a depth-first search, where the recursive function returns the serialization of the tree. At each node, record the result in a map, and analyze the map after to determine duplicate subtrees.

 1 class Solution {
 2     Map<String, Integer> count;
 3     List<TreeNode> ans;
 4     public List<TreeNode> findDuplicateSubtrees(TreeNode root) {
 5         count = new HashMap();
 6         ans = new ArrayList();
 7         collect(root);
 8         return ans;
 9     }
10
11     public String collect(TreeNode node) {
12         if (node == null) return "#";
13         String serial = node.val + "," + collect(node.left) + "," + collect(node.right);
14         count.put(serial, count.getOrDefault(serial, 0) + 1);
15         if (count.get(serial) == 2)
16             ans.add(node);
17         return serial;
18     }
19 }

原文地址:https://www.cnblogs.com/kexinxin/p/10383085.html

时间: 2024-11-09 06:38:58

Leetcode 652.寻找重复的子树的相关文章

寻找重复的子树(dfs)

题目连接: https://leetcode-cn.com/problems/find-duplicate-subtrees/ 题目大意: 中文题 具体思路: 将每一颗子树转换成字符串,然后通过unordered_map去重即可(map的速度较慢) AC代码: 1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right

LeetCode:寻找重复数【287】

LeetCode:寻找重复数[287] 题目描述 给定一个包含 n + 1 个整数的数组 nums,其数字都在 1 到 n 之间(包括 1 和 n),可知至少存在一个重复的整数.假设只有一个重复的整数,找出这个重复的数. 示例 1: 输入: [1,3,4,2,2] 输出: 2 示例 2: 输入: [3,1,3,4,2] 输出: 3 说明: 不能更改原数组(假设数组是只读的). 只能使用额外的 O(1) 的空间. 时间复杂度小于 O(n2) . 数组中只有一个重复的数字,但它可能不止重复出现一次.

POJ 3693 Maximum repetition substring (寻找重复次数最多的连续子串)

Maximum repetition substring Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9083   Accepted: 2782 Description The repetition number of a string is defined as the maximum number R such that the string can be partitioned into R same conse

LeetCode:存在重复元素【217】

LeetCode:存在重复元素[217] 题目描述 给定一个整数数组,判断是否存在重复元素. 如果任何值在数组中出现至少两次,函数返回 true.如果数组中每个元素都不相同,则返回 false. 示例 1: 输入: [1,2,3,1] 输出: true 示例 2: 输入: [1,2,3,4] 输出: false 示例 3: 输入: [1,1,1,3,3,4,3,2,4,2] 输出: true 题目分析 对于数据结构HashSet, 我们首先需要知道的是HashSet是不包含重复元素的,其次是存储

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

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

[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] Find Duplicate File in System 在系统中寻找重复文件

Given a list of directory info including directory path, and all the files with contents in this directory, you need to find out all the groups of duplicate files in the file system in terms of their paths. A group of duplicate files consists of at l

LeetCode: 153. 寻找旋转排序数组中的最小值(二分查找)

假设按照升序排序的数组在预先未知的某个点上进行了旋转. ( 例如,数组 [0,1,2,4,5,6,7] 可能变为 [4,5,6,7,0,1,2] ). 请找出其中最小的元素. 你可以假设数组中不存在重复元素. 示例 1: 输入: [3,4,5,1,2]输出: 1示例 2: 输入: [4,5,6,7,0,1,2]输出: 0 public int findMin(int[] nums) { int lo = 0, hi = nums.length - 1; while (lo < hi) { int

[LeetCode] Duplicate Emails 重复的邮箱

Write a SQL query to find all duplicate emails in a table named Person. +----+---------+ | Id | Email | +----+---------+ | 1 | [email protected] | | 2 | [email protected] | | 3 | [email protected] | +----+---------+ For example, your query should ret