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的更多相关文章

  1. [LeetCode] Find Duplicate Subtrees 寻找重复树

    Given a binary tree, return all duplicate subtrees. For each kind of duplicate subtrees, you only ne ...

  2. LeetCode - Find Duplicate Subtrees

    Given a binary tree, return all duplicate subtrees. For each kind of duplicate subtrees, you only ne ...

  3. 【LeetCode】652. Find Duplicate Subtrees 解题报告(Python)

    [LeetCode]652. Find Duplicate Subtrees 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...

  4. LeetCode 652: 寻找重复的子树 Find Duplicate Subtrees

    LeetCode 652: 寻找重复的子树 Find Duplicate Subtrees 题目: 给定一棵二叉树,返回所有重复的子树.对于同一类的重复子树,你只需要返回其中任意一棵的根结点即可. 两 ...

  5. [Swift]LeetCode652. 寻找重复的子树 | Find Duplicate Subtrees

    Given a binary tree, return all duplicate subtrees. For each kind of duplicate subtrees, you only ne ...

  6. [leetcode-652-Find Duplicate Subtrees]

    Given a binary tree, return all duplicate subtrees. For each kind of duplicate subtrees, you only ne ...

  7. 652. Find Duplicate Subtrees找出重复的子树

    [抄题]: 就是出现了多次的子树,可以只包括一个点. Given a binary tree, return all duplicate subtrees. For each kind of dupl ...

  8. LC 652. Find Duplicate Subtrees

    Given a binary tree, return all duplicate subtrees. For each kind of duplicate subtrees, you only ne ...

  9. Find Duplicate Subtrees

    Given a binary tree, return all duplicate subtrees. For each kind of duplicate subtrees, you only ne ...

随机推荐

  1. Android logcat详细用法

      admin| 2011-10-29 11:16| 分类:学习文档| Android logcat | 评论:[0]| Android日志系统提供了记录和查看系统调试信息的功能.日志都是从各种软件和 ...

  2. Aggregate (GROUP BY) Function Descriptions

    w Table 13.25 Aggregate (GROUP BY) Functions Name Description AVG() Return the average value of the ...

  3. Mysql日常操作

    创建用户并授权 grant all privileges on test.* to "test"@"localhost" identified by " ...

  4. mac配置python自然语言处理环境

    一.nltk安装 Ⅰ.工具安装步骤 1.根据python版本从 https://pypi.python.org/pypi/setuptools 下载对应版本的setuptools.然后,在终端下运行, ...

  5. golang 中的定时器(timer),更巧妙的处理timeout

    今天看到kite项目中的一段代码,发现挺有意思的. // generateToken returns a JWT token string. Please see the URL for detail ...

  6. [golang]内存不断增长bytes.makeSlice

    ------------------------------------------ 2015.7月更新 后面发现这里其实有一个sb的问题,在于内存回收和释放. 每个http请求,都会带一个http. ...

  7. linux内核获取当前进程路径分析

    一个简单的问题,·linux下获取当前进程.我们都知道在内核中获取当前进程可以利用current宏 #define get_current() (current_thread_info()->t ...

  8. 15.遇到window leaked的解决方法

    遇到这个可能是android:configChanges没有配置好 可以试试配置为这个 mcc|mnc|locale|touchscreen|keyboard|keyboardHidden|navig ...

  9. 《闪存问题之PROGRAM DISTURB》总结

    来自 http://www.ssdfans.com/?p=1814 SSD之所以需要BCH或LDPC等ECC纠错算法,是因为闪存中的数据会在神不知鬼不觉的情况下发生比特翻转. 导致比特翻转的原因很多, ...

  10. 8种主要排序算法的C#实现 (二)

    归并排序 归并排序也是采用“分而治之”的方式.刚发现分治法是一种算法范式,我还一直以为是一种需要意会的思想呢. 不好意思了,孤陋寡闻了,哈哈! 原理:将两个有序的数列,通过比较,合并为一个有序数列.  ...