LeetCode——Find Duplicate Subtrees
Question
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.
Solution
遍历所有子树的情况,遍历每棵子树的时候,采用先序遍历,但是得把空节点考虑进去,这样只有结构一样,遍历得到的字符串才一样。 也就是说,先序遍历(不考虑空孩子节点)的结果相同,并不意味着树的结构相同。 但是考虑了以后就是唯一的了。
Code
/**
* 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 {
public:
vector<TreeNode*> findDuplicateSubtrees(TreeNode* root) {
if (root == NULL)
return vector<TreeNode*>();
Trace(root);
Compare();
return res;
}
// 遍历所有子树
void Trace(TreeNode* root) {
if (root != NULL)
sub1.push_back(root);
if (root->left != NULL)
Trace(root->left);
if (root->right != NULL)
Trace(root->right);
}
void Compare() {
for (int i = 0; i < sub1.size(); i++) {
string tmp = "";
SubTreeStr(sub1[i], tmp);
if (count.find(tmp) == count.end()) {
count[tmp] = 1;
} else
count[tmp] += 1;
if (childs.find(tmp) == childs.end())
childs[tmp] = sub1[i];
}
map<string, int>::iterator iter;
for (iter = count.begin(); iter != count.end(); iter++) {
if (iter->second > 1)
res.push_back(childs[iter->first]);
}
}
void SubTreeStr(TreeNode* root1, string& str) {
// 考虑空节点,才能保证先序遍历的唯一性
if (root1 == NULL) {
str += "NULL";
} else {
str += to_string(root1->val);
SubTreeStr(root1->left, str);
SubTreeStr(root1->right, str);
}
}
vector<TreeNode*> sub1, res;
map<string, int> count;
map<string, TreeNode*> childs;
};
LeetCode——Find Duplicate Subtrees的更多相关文章
- [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
Given a binary tree, return all duplicate subtrees. For each kind of duplicate subtrees, you only ne ...
- 【LeetCode】652. Find Duplicate Subtrees 解题报告(Python)
[LeetCode]652. Find Duplicate Subtrees 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...
- LeetCode 652: 寻找重复的子树 Find Duplicate Subtrees
LeetCode 652: 寻找重复的子树 Find Duplicate Subtrees 题目: 给定一棵二叉树,返回所有重复的子树.对于同一类的重复子树,你只需要返回其中任意一棵的根结点即可. 两 ...
- [Swift]LeetCode652. 寻找重复的子树 | Find Duplicate Subtrees
Given a binary tree, return all duplicate subtrees. For each kind of duplicate subtrees, you only ne ...
- [leetcode-652-Find Duplicate Subtrees]
Given a binary tree, return all duplicate subtrees. For each kind of duplicate subtrees, you only ne ...
- 652. Find Duplicate Subtrees找出重复的子树
[抄题]: 就是出现了多次的子树,可以只包括一个点. Given a binary tree, return all duplicate subtrees. For each kind of dupl ...
- LC 652. Find Duplicate Subtrees
Given a binary tree, return all duplicate subtrees. For each kind of duplicate subtrees, you only ne ...
- Find Duplicate Subtrees
Given a binary tree, return all duplicate subtrees. For each kind of duplicate subtrees, you only ne ...
随机推荐
- 忽略UserInterfaceState.xcuserstate
GIT忽略iOS项目UserInterfaceState.xcuserstate 1.删除仓库中跟踪的UserInterfaceState.xcuserstate git rm --cached ...
- PHP函数addslashes和mysql_real_escape_string的区别
转自:http://www.jb51.net/article/49205.htm 这篇文章主要介绍了PHP函数addslashes和mysql_real_escape_string的区别,以及一个 ...
- node.js使用require给flume提交请求
node.js使用require给flume提交请求 - 简书 https://www.jianshu.com/p/02c20e2d011a 玄月府的小妖在debug 关注 2017.04 ...
- 1.1 - python基础语法 - 总结练习题
1.编译型与解释型语言的区别,哪些属于编译型,哪些属于解释型 编译型:c/c++/go 运行速度快,开发效率低,不可跨平台 解释型:python/java/php/ruby 运行速度低,开发效率高,可 ...
- Spring Data 之 Repository 接口
1. 介绍 Repository是一个空接口,即是一个标记性接口; 若我们定义的接口继承了Repository,则该接口会被IOC容器识别为一个 Repository Bean; 也可以通过@Repo ...
- js的class属性获取、增加、移除
2018年4月10日,北京城的第三份工作已经开始,坚信自己在这里能学到很多,加油! 贴代码,昨天回顾了一点js知识: <script> $(function(){ //赋予一个点击事件 $ ...
- MySQL优化(一):MySQL分库分表
一.分库分表种类 1.垂直拆分 在考虑数据拆分的时候,一般情况下,应该先考虑垂直拆分.垂直可以理解为分出来的库表结构是互相独立各不相同的. - 如果有多个业务,每个业务直接关联性不大,那么就可以把每个 ...
- jquery prop attr
checked比较特殊,只要设置了属性checked,不管何值都是checked的.例如:<input type="checkbox" checked><inpu ...
- BroadcastReceiver 翻译
1. 动态注册与退出 If registering a receiver in your Activity.onResume() implementation, you should unregist ...
- 前端 html border-right: 1px solid red;
后边框 加粗 实体线 红色 border-right: 1px solid red;