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 ...
随机推荐
- MG301使用笔记
[1]模块接收到的数据为16进制,显示乱码 配置命令:AT^IOMODE=1,1 设置对接收数据进行转换,当对端以 hex 格式发送数据,必须使用数据转换,否则数据无法完全上报.必须禁止使用缓存区.
- 2.NumPy简介
一:NumPy简介 • 官网链接:http://www.numpy.org/ • NumPy教程链接:https://www.yiibai.com/numpy/ • NumPy是Python语言的一个 ...
- PAT Basic 1030 完美数列 (25 分)
给定一个正整数数列,和正整数 p,设这个数列中的最大值是 M,最小值是 m,如果 M≤mp,则称这个数列是完美数列. 现在给定参数 p 和一些正整数,请你从中选择尽可能多的数构成一个完美数列. 输入格 ...
- Lambda方法推导(method references)
在上一篇[http://www.cnblogs.com/webor2006/p/7707281.html]中提到了方法推导的东东: 这里说细的学习一下它,下面走起! Method references ...
- Summer training #4
D:找到两个数 一个是另一个的整数倍(1也算) 因为N是600000 调和级数为ln(n+1) 算一下 可以直接爆 #include <bits/stdc++.h> #include &l ...
- SAP选择屏幕下拉框实现
DATA:vid TYPE vrm_id , "屏幕字段(可以是单个的I/O空间或者是Table Control中的一个单元格) list TYPE vrm_values, value LI ...
- 第七章 路由 70 路由-vue-router的基本使用
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8&quo ...
- Oracle查询死锁
select sample_time,session_id,sql_id,event,sql_plan_hash_value,blocking_session from dba_hist_active ...
- 【51nod 1847】奇怪的数学题
题目描述 给出 N,K ,请计算下面这个式子: \(∑_{i=1}^N∑_{j=1}^Nsgcd(i,j)^k\) 其中,sgcd(i, j)表示(i, j)的所有公约数中第二大的,特殊地,如果gcd ...
- 【BD2】- Linux安装db2 v11.1
一.背景 项目需要兼容多种数据库oracle.mysql.db2.KingDB等等...... 自己在阿里云买的服务器安装测试...... 二.记录安装过程 1.创建db2目录 [root@ysx y ...