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 sleep 死锁例子
表结构 CREATE TABLE `orders` ( `order_id` int(11) NOT NULL, `order_addr` varchar(255) DEFAULT NULL ) EN ...
- php 随笔 截取字符串 跳出循环 去除空格 修改上传文件大小限制
substr(string,start,length) echo substr("Hello world",6); world 跳出循环 for($i=1; $i<5; $i ...
- vlan linux内核数据流程
转:http://blog.sina.com.cn/s/blog_62bbc49c0100fs0n.html 一.前言 前几天做协议划分vlan的时候看了一些linux内核,了解不深,整理了下vlan ...
- Fragment 和Acitivity的相互传值
百度云:链接: http://pan.baidu.com/s/1jGzYRFg 密码: xpx9
- zabbix 自定义Key (六)
1.在zabbix_agent端zabbix_agentd.conf配置文件中增加自定义Key(/usr/local/zabbix_agent/etc/zabbix_agentd.conf) ### ...
- nacos 1.1.x 集群部署笔记
Nacos 是什么? https://nacos.io/zh-cn/docs/what-is-nacos.html 服务(Service)是 Nacos 世界的一等公民.Nacos 支持几乎所有主流类 ...
- c++第四次作业:类的继承
一.定义 类的继承:是从新的类从已有类那里得到已有的特性. 二.方式 1.公有继承:当类的继承方式为公有继承时,基类的公有成员和保护成员的访问属性在派生类中不变,而基类的私有成员不可直接访问. 例: ...
- git fetch和pull的区别
Git中从远程的分支获取最新的版本到本地有这样2个命令: 1. git fetch:相当于是从远程获取最新版本到本地,不会自动merge 1 2 3 Git fetch origin master ...
- poj3691 DNA repair[DP+AC自动机]
$给定 n 个模式串,和一个长度为 m 的原串 s,求至少修改原串中的几个字符可以使得原串中不包含任一个模式串.模式串总长度 ≤ 1000,m ≤ 1000.$ 先建出模式串的AC自动机,然后考虑怎么 ...
- 帝都之行9day:正式上班第一天
今天是我正式上班的第一天. 面了两天,三家公司,然后周五就去办入职了,我是不是太随便了点,捂脸. 不管怎么说,又要开始上班啦,CRUD的日子又要开始了…… 加油吧!