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 one of 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 listn.
分析:把每个node当成root,然后得到它的preorder traversal string, 因为子node的preorder traversal string是可以被用在parent上的,这样时间复杂度只是O(n).
class Solution {
public List<TreeNode> findDuplicateSubtrees(TreeNode root) {
List<TreeNode> res = new LinkedList<>();
preorder(root, new HashMap<>(), res);
return res;
}
public String preorder(TreeNode cur, Map<String, Integer> map, List<TreeNode> res) {
if (cur == null) return "#";
String serial = cur.val + "," + preorder(cur.left, map, res) + "," + preorder(cur.right, map, res);
if (map.getOrDefault(serial, ) == ) res.add(cur);
map.put(serial, map.getOrDefault(serial, ) + );
return serial;
}
}
Find Duplicate Subtrees的更多相关文章
- [LeetCode] Find Duplicate Subtrees 寻找重复树
Given a binary tree, return all duplicate subtrees. For each kind of duplicate subtrees, you only ne ...
- [Swift]LeetCode652. 寻找重复的子树 | 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 - Find Duplicate Subtrees
Given a binary tree, return all duplicate subtrees. For each kind of duplicate subtrees, you only ne ...
- LeetCode——Find Duplicate Subtrees
Question Given a binary tree, return all duplicate subtrees. For each kind of duplicate subtrees, yo ...
- [leetcode-652-Find Duplicate Subtrees]
Given a binary tree, return all duplicate subtrees. For each kind of duplicate subtrees, you only ne ...
- LeetCode 652: 寻找重复的子树 Find Duplicate Subtrees
LeetCode 652: 寻找重复的子树 Find Duplicate Subtrees 题目: 给定一棵二叉树,返回所有重复的子树.对于同一类的重复子树,你只需要返回其中任意一棵的根结点即可. 两 ...
- LC 652. Find Duplicate Subtrees
Given a binary tree, return all duplicate subtrees. For each kind of duplicate subtrees, you only ne ...
- 【LeetCode】652. Find Duplicate Subtrees 解题报告(Python)
[LeetCode]652. Find Duplicate Subtrees 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...
随机推荐
- jquery blur()函数 语法
jquery blur()函数 语法 作用:当元素失去焦点时发生 blur 事件.blur() 函数触发 blur 事件,或者如果设置了 function 参数,该函数也可规定当发生 blur 事件时 ...
- Navicat for MySQL 使用
库创建标准 表查看sql样式
- docker安装中文版Gitlab服务端
1.pull中文版镜像: docker pull beginor/gitlab-ce:11.3.0-ce.0 2.创建目录: 通常会将 GitLab 的配置 (etc) . 日志 (log) .数据 ...
- [BZOJ2730]:[HNOI2012]矿场搭建(塔尖)
题目传送门 题目描述 煤矿工地可以看成是由隧道连接挖煤点组成的无向图.为安全起见,希望在工地发生事故时所有挖煤点的工人都能有一条出路逃到救援出口处.于是矿主决定在某些挖煤点设立救援出口,使得无论哪一个 ...
- Java file.encoding
1. file.encoding属性的作用 file.encoding 的值是整个程序使用的编码格式. 可以使用 System.out.println(System.getProperty(&quo ...
- TCP主动打开 之 第二次握手-接收SYN+ACK
假设客户端执行主动打开,已经经过第一次握手,即发送SYN包到服务器,状态变为SYN_SENT,服务器收到该包后,回复SYN+ACK包,客户端收到该包,进行主动打开端的第二次握手部分:流程中涉及到的函数 ...
- 2 大O表示法
1.大O表示法 表示程序的执行时间或占用空间随数据规模的增长趋势. 算法操作 时间复杂度 线性查找 O(n) 二分查找 O(logn) 无序数组插入 O(1) 无序数组删除 O(n) 有序数组插入 O ...
- 尚学堂requireJs课程---2、模块
尚学堂requireJs课程---2.模块 一.总结 一句话总结: # 将代码以及使用放到独立的闭包中去,并且赋值给了变量,便于外部访问 # return出了函数和变量(放在一个json对象中) # ...
- koa 应用生成器
通过应用 koa 脚手架生成工具 可以快速创建一个基于 koa2 的应用的骨架 1.全局安装 npm install koa-generator -g 2.创建项目 koa koa_demo 3.安装 ...
- leetcode334 递增的三元子序列
class Solution { public: bool increasingTriplet(vector<int>& nums) { //使用双指针: int len=nums ...