寻找重复的子树 Find Duplicate Subtrees
2018-07-29 17:42:29
问题描述:
问题求解:
本题是要求寻找一棵树中的重复子树,问题的难点在于如何在遍历的时候对之前遍历过的子树进行描述和保存。
这里就需要使用之前使用过的二叉树序列化的手法,将遍历到的二叉树进行序列化表达,我们知道序列化的二叉树可以唯一的表示一棵二叉树,并可以用来反序列化。
想到这里其实问题就已经解决了一大半了, 我们只需要在遍历的过程中将每次的序列化结果保存到一个HashMap中,并对其进行计数,如果重复出现了,那么将当前的节点添加到res中即可。
public List<TreeNode> findDuplicateSubtrees(TreeNode root) {
List<TreeNode> res = new ArrayList<>();
Map<String, Integer> map = new HashMap<>();
helper(root, map, res);
return res;
} private String helper(TreeNode root, Map<String, Integer> map, List<TreeNode> res) {
if (root == null) return "#";
String str = root.val + "," + helper(root.left, map, res) + "," + helper(root.right, map, res);
if (map.containsKey(str)) {
if (map.get(str) == 1) res.add(root);
map.put(str, map.get(str) + 1);
}
else map.put(str, 1);
return str;
}
算法改进:
上述的算法基本可以在O(n)的时间复杂度解决问题,但是其中的字符串拼接是非常耗时的,这里可以对这个部分做出一点改进。如果我们不用序列化结果来表征一个子树,用一个id值来表征的话,那么就可以规避掉字符串拼接的问题。
这里直接使用id的size来进行id的分配,值得注意的是由于已经给null分配了0,那么每次分配的大小应该是size + 1。
public List<TreeNode> findDuplicateSubtrees(TreeNode root) {
List<TreeNode> res = new ArrayList<>();
Map<Long, Integer> id = new HashMap<>();
Map<Integer, Integer> map = new HashMap<>();
helper(root, id, map, res);
return res;
} private Integer helper(TreeNode root, Map<Long, Integer> id, Map<Integer, Integer> map, List<TreeNode> res) {
if (root == null) return 0;
Long key = ((long) root.val << 32) | helper(root.left, id, map, res) << 16 | helper(root.right, id, map, res);
if (!id.containsKey(key))
id.put(key, id.size() + 1);
int curId = id.get(key);
if (map.containsKey(curId)) {
if (map.get(curId) == 1) res.add(root);
map.put(curId, map.get(curId) + 1);
}
else map.put(curId, 1);
return curId;
}
寻找重复的子树 Find Duplicate Subtrees的更多相关文章
- LeetCode 652: 寻找重复的子树 Find Duplicate Subtrees
LeetCode 652: 寻找重复的子树 Find Duplicate Subtrees 题目: 给定一棵二叉树,返回所有重复的子树.对于同一类的重复子树,你只需要返回其中任意一棵的根结点即可. 两 ...
- [Swift]LeetCode652. 寻找重复的子树 | Find Duplicate Subtrees
Given a binary tree, return all duplicate subtrees. For each kind of duplicate subtrees, you only ne ...
- Leetcode 652.寻找重复的子树
寻找重复的子树 给定一棵二叉树,返回所有重复的子树.对于同一类的重复子树,你只需要返回其中任意一棵的根结点即可. 两棵树重复是指它们具有相同的结构以及相同的结点值. 下面是两个重复的子树: 因此,你需 ...
- Java实现 LeetCode 652 寻找重复的子树(两个map的DFS)
652. 寻找重复的子树 给定一棵二叉树,返回所有重复的子树.对于同一类的重复子树,你只需要返回其中任意一棵的根结点即可. 两棵树重复是指它们具有相同的结构以及相同的结点值. 示例 1: 1 / \ ...
- [LeetCode] Find Duplicate Subtrees 寻找重复树
Given a binary tree, return all duplicate subtrees. For each kind of duplicate subtrees, you only ne ...
- 652. Find Duplicate Subtrees找出重复的子树
[抄题]: 就是出现了多次的子树,可以只包括一个点. Given a binary tree, return all duplicate subtrees. For each kind of dupl ...
- 【LeetCode】652. Find Duplicate Subtrees 解题报告(Python)
[LeetCode]652. Find Duplicate Subtrees 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...
- LeetCode——Find Duplicate Subtrees
Question Given a binary tree, return all duplicate subtrees. For each kind of duplicate subtrees, yo ...
- Leetcode之二分法专题-287. 寻找重复数(Find the Duplicate Number)
Leetcode之二分法专题-287. 寻找重复数(Find the Duplicate Number) 给定一个包含 n + 1 个整数的数组 nums,其数字都在 1 到 n 之间(包括 1 和 ...
随机推荐
- 014-配置SSH免密钥登录
问题:client端需要免密钥登录服务器server如何配置?1.前提:客户端已安装openssh-client;服务端已安装openssh-server;服务器端22号端口已经打开2.需要密钥登录时 ...
- 33. Search in Rotated Sorted Array(二分查找)
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e. ...
- Python: 字典列表: 通过某个字段将记录分组
问题:有一个字典或者实例的序列,想根据某个特定的字段比如date 来分组迭代访问. answer: itertools.groupby函数对于这样的数据分组操作非常实用 eg: rows = [{'a ...
- redis安装及注意事项
在linux中使用wget时,若报-bash: wget: command not found,则表明没有安装wget,需要安装,安装命令如下: yum -y install wget 安装完成即可以 ...
- 机器学习、深度学习以及人工智能正在快速演进(ML、DL、AI)
机器学习.深度学习以及人工智能正在快速演进 机器学习.深度学习和人工智能(ML.DL和AI)是彼此相关的概念,他们正在改变不知多少行业,改变其自身管理模式,同时改变做出决策的方式.显然,ML.DL和A ...
- MongoDB ----基于分布式文件存储的数据库
参考: http://www.cnblogs.com/huangxincheng/category/355399.html http://www.cnblogs.com/daizhj/category ...
- C++中公有继承、保护继承、私有继承的区别
公有继承时基类中各成员属性保持不变,基类中private成员被隐藏.派生类的成员只能访问基类中的public/protected成员,而不能访问private成员:派生类的对象只能访问基类中的publ ...
- P3374 【模板】树状数组 1(cdq)
P3374 [模板]树状数组 1 cdq分治 刚学了cdq分治(dyf神犇强力安利下),发现可以做这种题,当然是来试水了(逃 cdq好像只能离线的样子 cdq分治(转) 以下是摘录的几句: 在合并的时 ...
- Python3 Selenium自动化测试赋值出现:WebDriverException: Message: unknown error: call function result missing 'value'
Python3 Selenium自动化测试赋值出现:WebDriverException: Message: unknown error: call function result missing ' ...
- ubuntu 更改python3为默认版本
ubuntu 自带两个python版本,一个是python2一个是python3 默认版本是python2的,想要更改ubuntu python3 为默认版本, 只需要两行命令: sudo updat ...