LC 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 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 list.
Runtime: 40 ms, faster than 18.69% of C++ online submissions for 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 {
private:
unordered_map<string,int> map;
public:
vector<TreeNode*> findDuplicateSubtrees(TreeNode* root) {
vector<TreeNode*> ret;
helper(root, ret);
//for(auto it : map) cout << it.first << endl;
return ret;
}
string helper(TreeNode* root, vector<TreeNode*> & ret){
if(!root) return "";
string rootval = to_string(root->val);
string tmp = rootval;
if(!root->left && root->right){
tmp = rootval + " Null " + helper(root->right, ret);
}else if(root->left && !root->right){
tmp = rootval + " " + helper(root->left,ret) + " Null ";
} else if (root->left && root->right){
tmp = rootval + " " + helper(root->right,ret) + " " + helper(root->left,ret);
}
//if(root->val == 4) cout << tmp << endl;
if(map.count(tmp)) {
if(map[tmp] == ) {
ret.push_back(root);
map[tmp]++;
}
}else {
map[tmp] = ;
}
return tmp;
}
};
下面是写的比较顺的一种。
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/ // We can serialize each subtree. 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.
class Solution {
public:
vector<TreeNode*> findDuplicateSubtrees(TreeNode* root) { //store count of each serialized tree
unordered_map<string, int>mymap;
vector<TreeNode*> res; DFS(root,mymap,res);
return res;
} string DFS(TreeNode* root, unordered_map<string, int> &mymap, vector<TreeNode*> &res){
if(!root){
return "#";
} string s = to_string(root->val) + "," + DFS(root->left, mymap, res) + "," + DFS(root->right, mymap, res);
if(++mymap[s]==)
res.push_back(root);
return s;
}
};
更有人用了bit,惊了。
long key=((static_cast<long>(node->val))<< | helper(node->left, ans)<< | helper(node->right, ans));
LC 652. Find Duplicate Subtrees的更多相关文章
- 【LeetCode】652. Find Duplicate Subtrees 解题报告(Python)
[LeetCode]652. Find Duplicate Subtrees 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...
- 652. Find Duplicate Subtrees找出重复的子树
[抄题]: 就是出现了多次的子树,可以只包括一个点. Given a binary tree, return all duplicate subtrees. For each kind of dupl ...
- 652. Find Duplicate Subtrees
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode ...
- [LeetCode]652. Find Duplicate Subtrees找到重复树
核心思想是:序列化树 序列化后,用String可以唯一的代表一棵树,其实就是前序遍历改造一下(空节点用符号表示): 一边序列化,一边用哈希表记录有没有重复的,如果有就添加,注意不能重复添加. 重点就是 ...
- LeetCode 652: 寻找重复的子树 Find Duplicate Subtrees
LeetCode 652: 寻找重复的子树 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 ...
- 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 ...
随机推荐
- MySql学习笔记【三、表相关操作】
创建表 CREATE TABLE [IF NOT EXISTS] table_name( column_name data_type, ... ) 如: CREATE TABLE test_table ...
- 第一章·MySQL介绍及安装
一.DBA工作内容及课程体系 二.MySQL课程体系介绍 三.DBA的职业素养 四.MySQL简介及安装 4.1 什么是数据? 数据(data)是事实或观察的结果,是对客观事物的逻辑归纳,是用于表示客 ...
- 最终章·MySQL从入门到高可用架构报错解决
1. 报错原因:MySQL的socket文件目录不存在. 解决方法:创建MySQL的socket文件目录 mkdir /application/mysql-5.6.38/tmp 2. 报错原因:soc ...
- Linux工具之watch
watch watch 监测一个命令的运行结果 -n 指定间隔的时间 -d watch会高亮显示变化的区域. -t 会关闭watch命令在顶部的时间间隔, ...
- python常用模块:标准文件及模块练习
1.请写出规范目录 并解释各文件夹的作用 bin 执行文件core 核心业务逻辑conf 配置文件lib 库.公共代码.第三方模块db 数据分析log 日志文件readme 文本文档 2.改造atm+ ...
- CSS基础学习-11.CSS伸缩盒(新版本)
- zzzphp V1.6.0 按照功能分析漏洞
0 基础支撑功能 0.1 路由功能 0.2 模版解析 * zzzphp V1.6.0 的代码执行漏洞,模版解析功能的问题 程序解析模版时,将模版中的部分内容匹配出来直接传递给了eval,且没有经过过滤 ...
- solr admin界面的监控
这里可以看到,solr的版本,lucene的版本,jvm的版本,CPU核数,jvm启动参数,还有物理内存占用,交换空间占用,jvm内存占用. 这里可以看到每个core的情况. 这里可以看到java的所 ...
- pycharm 安装 tensorflow
1. 安装python 3.5 链接:https://www.python.org/downloads/release/python-352/ 1.1如果之前安装了其他版本的,可以在你需要的项目中, ...
- --print-defaults打印mysqld启动加载配置
Mysql启动配置文件加载路径 Mysql可以读取到的配置文件 /etc/my.cnf /etc/mysql/my.cnf /usr/local ...