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 ...
随机推荐
- netty 网关 flume 提交数据 去除透明 批处理 批提交 cat head tail 结合 管道显示行号
D:\javaNettyAction\NettyA\src\main\java\com\test\HexDumpProxy.java package com.test; import io.netty ...
- Static Import Constant interface
Static Import https://docs.oracle.com/javase/1.5.0/docs/guide/language/static-import.html In order t ...
- varints
Protocol Buffer技术详解(数据编码) - Stephen_Liu - 博客园 https://www.cnblogs.com/stephen-liu74/archive/2013/01/ ...
- Spark源码分析 – DAGScheduler
DAGScheduler的架构其实非常简单, 1. eventQueue, 所有需要DAGScheduler处理的事情都需要往eventQueue中发送event 2. eventLoop Threa ...
- String Problem --- hdu3374(kmp、字典序最大与最小)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3374 题意很简单,输出的是最小字典序的编号,最小字典序个数,最大字典序编号,最大字典序个数. 可以想一 ...
- Python高级特性(3): Classes和Metaclasses(转)
原文:Python高级特性(3): Classes和Metaclasses 类和对象 类和函数一样都是Python中的对象.当一个类定义完成之后,Python将创建一个“类对象”并将其赋值给一个同名变 ...
- Redis常见操作
1. 对于key的所有操作 del key1 key2 … keyn 作用:删除1个或者多个键返回值:不存在的key忽略掉,返回真正删除的key的数量 rename key newkey 作用:给ke ...
- 目标检测-Faster R-CNN
[目标检测]Faster RCNN算法详解 Ren, Shaoqing, et al. “Faster R-CNN: Towards real-time object detection with r ...
- bat批处理异备文件、压缩文件
1.压缩本地文件,并把压缩后的文件复制到其他机器 net use Z: \\192.168.135.1\share_linux a123456! /user:chaoqun.guo set bath= ...
- 将 ssh (security shell) 移植到 vxworks
openssh 依赖 openssl,这两个东西主要针对posix系统,移植到 vxworks 等实时系统有相当的难度. 可以考虑移植如下的库(ssh server): dropbear: https ...