寻找重复的子树(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;
 7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 8  * };
 9  */
10 class Solution {
11 public:
12     vector<TreeNode*> findDuplicateSubtrees(TreeNode* root) {
13
14         vector<TreeNode*>ans;
15         unordered_map<string,int>vis;
16
17         dfs(root, ans, vis);
18
19         return ans;
20     }
21     string dfs(TreeNode* root, vector<TreeNode*>&ans, unordered_map<string,int>&vis){
22
23         if(root == NULL)
24             return "#";
25
26         string tmp = to_string(root->val) + dfs(root->left, ans, vis) + dfs(root->right, ans, vis);
27
28         if(vis[tmp] == 1)
29             ans.push_back(root);
30         vis[tmp]++;
31
32         return tmp;
33     }
34 };

原文地址:https://www.cnblogs.com/letlifestop/p/11517008.html

时间: 2024-08-30 17:09:38

寻找重复的子树(dfs)的相关文章

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

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

九章算法面试题20 寻找重复的URL

九章算法官网-原文网址 http://www.jiuzhang.com/problem/20/ 题目 给定A.B两个大文件,各存放50亿个url,每个url各占256字节,内存限制是4G,让你找出同时在A和B中出现的url. 解答 方法1:使用BloomFilter(一种类似于hash表但比hash表占用空间更小的查重数据结构),通过K个不同的hash函数,将5G个URL映射到32G个bit位上,当且仅当K个hash函数得到的bit位上都是1时,代表该url重复出现.一般来讲K取8.该方法存在精

633. 寻找重复的数

给出一个数组 nums 包含 n + 1 个整数,每个整数是从 1到 n (包括边界),保证至少存在一个重复的整数.假设只有一个重复的整数,找出这个重复的数. 注意事项 1.不能修改数组(假设数组只能读)2.只能用额外的O(1)的空间3.时间复杂度小于O(n^2)4.数组中只有一个重复的数,但可能重复超过一次 样例 给出 nums = [5,5,4,3,2,1],返回 5.给出 nums = [5,4,4,3,2,1],返回 4. 1 int findDuplicate(vector<int>

[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

lduan server 2012 重复数据删除 DFS合名空间 服务上 (十二)

leet

# 题名1 两数之和    2 两数相加    3 无重复字符的最长子串    4 寻找两个有序数组的中位数    5 最长回文子串    6 Z 字形变换    7 整数反转    8 字符串转换整数 (atoi)    9 回文数    10 正则表达式匹配    11 盛最多水的容器    12 整数转罗马数字    13 罗马数字转整数    14 最长公共前缀    15 三数之和    16 最接近的三数之和    17 电话号码的字母组合    18 四数之和    19 删除链表

【Luogu】P1122最大子树和(DFS,树上DP)

题目链接 感觉自己DP好烂啊   道道看题解 钦定1为根,DFS搜索子树权值.如果子树权值大于0就将当前节点加上子树权值,反之就把子树扔掉.最后在所有节点的权值中寻找最优解. void dfs(int x,int fa){ if(f[x]>0) return; f[x]+=que[x]; for(int i=head[x];i;i=edge[i].next){ int to=edge[i].to; if(to==fa) continue; dfs(to,x); if(f[to]>0) f[x]