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 ...
随机推荐
- centos8下jdk13和tomcat9的安装
首先下载JDK13和tomcat9在对应的官网上: 通过xftp传到linux服务器上的对应的目录,如/usr/local apache-tomcat-9.0.27.tar.gz ,jdk-13.0 ...
- 【Day1】2.安装运行Python
视频地址(全部) https://edu.csdn.net/course/detail/26057 课件地址(全部) https://download.csdn.net/download/gentl ...
- 逆向破解 H.Koenig 遥控器 Part 1
逆向破解 H.Koenig 遥控器(Part 1) 最近我正在尝试一研究些自动吸尘器机器人.iRobot公司的Roomba貌似是该领域的领导者,但是作为实验来讲的话这些东西真是太昂贵了,我也找不到 ...
- hadoop--大数据生态圈中最基础、最重要的组件
hadoop是什么? hadoop是一个由Apache基金会所开发的分布式系统基础架构,hdfs分布式文件存储.MapReduce并行计算.主要是用来解决海量数据的存储和海量数据的分析计算问题,这是狭 ...
- java开发技巧
1,IDEA辅助功能Shift +F2去到有错误的地方Alt+Enter,会给出解决错误的建议: 2,调试,没问题的步骤,直接跳过,不要跳入细节: 调试时,要明确要跟踪的变量,不要陷入混乱: 3,调试 ...
- 清除LabVIEW中波形图表或波形图中的历史数据
清除LabVIEW中波形图表或波形图中的历史数据 方法一: 前面板中右键单击波形图表或波形图,选择数据操作>>清除图表或数据操作>>清除图形 方法二:(编程方法) 用于清除图表 ...
- python+request+HTMLTestRunner+unittest接口自动化测试框架
转自https://my.oschina.net/u/3041656/blog/820023 正在调研使用python进行自动化测试,在网上发现一篇比较好的博文,作者使用的是python3,但目前自己 ...
- linux内核 概念
内核Kernel只是操作系统的一部分,操作系统本身在内核之上还包含命令行shell和其他种类的用户界面. 通常内核包含: 中断服务程序 进程管理和调度程序 内存管理程序 进程同步方法 IO和设备等 在 ...
- mysqldump恢复
mysqldump的恢复操作比较简单,因为备份的文件就是导出的SQL语句,一般只需要执行这个文件就可以了,可以通过以下的方法. 方法一 [root@zstedu andyxi3306]# mysql ...
- Java-DatabaseConnectionPool工具类
package org.zxjava.test; import java.sql.Connection; import java.sql.DatabaseMetaData; import java.s ...