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 ...
随机推荐
- Ckeditor事件绑定
最近有个需求是要在点击CKeditor的时候触发某个判断的事件.试了一些方法都不可行,自己写的onclick时间都会被编辑器屏蔽.可以对对象加载完成绑定事件代码如下. CKEDITOR.instanc ...
- TCP和UDP的区别?
答:TCP提供面向连接的.可靠的数据流传输,而UDP提供的是非面向连接的.不可靠的数据流传输.TCP传输单位称为TCP报文段,UDP传输单位称为用户数据报.TCP注重数据安全性,UDP数据传输快,因为 ...
- sigmoid & softmax、cross-entropy、relu
sigmoid函数由于其自身特点,容易造成几个问题 1.当sigmoid在输出层时容易造成loss较大时学习速度慢的情况(或者说是代价函数学习速度衰退问题) 原因:神经网络的学习方式是通过求代价函数对 ...
- 使用JCONSOLE远程监控JVM
启动JMS服务 JConsole是从Java 5中开始引入的一个用于对JVM性能和资源消耗进行监控的图形化工具.JConsole可以连接本地的Java程序,也可以连接远程的Java程序.由于是GUI的 ...
- python学习笔记——字符串
类方法string.upper(str)需要引入string模块,实例方法str.upper()不需要引入string模块 无与伦比的列表解析功能 # coding=utf-8 # 列表解析 prin ...
- pytorch调参经验(一)
个人博客:https://yifdu.github.io/2018/11/18/pytorch%E8%B0%83%E5%8F%82%E7%BB%8F%E9%AA%8C%EF%BC%88%E4%B8%8 ...
- 单舵轮(叉车)AGV里程计数据解算
单舵轮(叉车)AGV里程计数据解算 2016-07 单舵轮AGV,一般包含一个驱动轮和两个从动轮,驱动轮是同时具备行走和转向两个功能的舵轮,因此,单舵轮AGV的运动学自由度为2个.舵轮线速度V1,舵轮 ...
- python与c语言交互应用实例
1.python向c语言写数据 1) 先将接收端编译成一个共享链接库gcc/arm-linux-gnueabihf-gcc -o bluetooth_proxy.so -shared -fPIC bl ...
- java通过URL获取文本内容
原文地址https://www.cnblogs.com/myadmin/p/7634262.html public static String readFileByUrl(String urlStr) ...
- 使用LocationManager来获取移动设备所在的地理位置信息
在Android应用程序中,可以使用LocationManager来获取移动设备所在的地理位置信息.看如下实例:新建android应用程序TestLocation. 1.activity_main.x ...