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 ...
随机推荐
- docker一键搭建Nginx+PHP环境(含自动部署命令)
文章的主要部分是一步一步的教程,文章的最后是我整理好的一键安装命令,自动下载并安装docker,构建镜像,启动容器集群(压缩包内注释覆盖范围达到80%) 大家可以看完教程亲自尝试下,也可以直接执行一键 ...
- SQL语句复习【专题八】
SQL语句复习[专题八] 序列 Sequence.数据库对象是 oracle 专有的.作用:可以将某一列的值使用序列,来实现自动增长的功能.访问序列的值.[序列有两个属性 nextval currva ...
- VS2012中--查找定义后从未被使用的函数
操作步骤如下: 选择项目==>右键属性==>代码分析(选择Microsoft的所有规则) 注:默认为 托管建议规则 注:CA1804 CA1811规则 例如需要查看某个项目从未被使用的函数 ...
- Linux rpm yum
RPM : 1 rpm -q 子选项 软件名 -a :列出已安装所有的软件包 -i :查看指定软件的详细信息 -l:查看指定软件的文件安装清单 -f:查看某个目录.文件是哪个包带来的 rpm -q ...
- Tarjan无向图的割点和桥(割边)全网详解&算法笔记&通俗易懂
更好的阅读体验&惊喜&原文链接 感谢@yxc的腿部挂件 大佬,指出本文不够严谨的地方,万分感谢! Tarjan无向图的割点和桥(割边) 导言 在掌握这个算法前,咱们有几个先决条件. [ ...
- Linux日常之命令awk
参考:http://www.zsythink.net/archives/tag/awk/ 一. 命令awk简介 1. awk是一种编程语言,用于对文本和数据进行处理的 2. 具有强大的文本格式化能力 ...
- 安装tensorflow遇到:Your CPU supports instructions that this TensorFlow binary was not compiled to use
为了提升CPU计算速度的.若你有支持cuda的GPU,则可以忽略这个问题,因为安装SSE4.1, SSE4.2, AVX, AVX2, FMA, 仅仅提升CPU的运算速度(大概有3倍). 解决方法: ...
- axios时遇到的Request Method: OPTIONS
前言 在请求axios 请求数据的时候,会出现options的,是因为请求是分为简单请求和复杂请求. 简单请求 满足下面两个条件的请求是简单请求: 请求方式是以下三种之一: HEAD GET POST ...
- 1012 最大公约数和最小公倍数问题 2001年NOIP全国联赛普及组
1012 最大公约数和最小公倍数问题 2001年NOIP全国联赛普及组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 白银 Silver 题目描述 Description 输入二个 ...
- HFUUOJ1023 闷声发大财 概率dp
题意 xyq有\(n\)个骰子,第\(i\)个骰子有\(a_i\)面,每次xyq都会把\(n\)个骰子搞一遍,其中的最小值作为结果,问最终结果的期望\(\mod (10^9+7 )\). 分析 lfx ...