652. 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 {
public:
    unordered_map<string, vector<TreeNode*>> s;
    vector<TreeNode*> findDuplicateSubtrees(TreeNode* root) {
        dfs(root);
        vector<TreeNode*> res;
        for (auto & i : s) {
            if (i.second.size() > 1)
                res.push_back(i.second[0]);
        }
        return res;
    }
    string dfs(TreeNode* root) {
        if (root == NULL)   return "";
        string t = "(" + dfs(root->left) + to_string(root->val) + dfs(root->right) + ")";
        s[t].push_back(root);
        return t;
    }
};

原文地址:https://www.cnblogs.com/JTechRoad/p/10052138.html

时间: 2024-09-29 09:54:46

652. Find Duplicate Subtrees的相关文章

[LeetCode]652. Find Duplicate Subtrees找到重复树

核心思想是:序列化树 序列化后,用String可以唯一的代表一棵树,其实就是前序遍历改造一下(空节点用符号表示): 一边序列化,一边用哈希表记录有没有重复的,如果有就添加,注意不能重复添加. 重点就是序列化树,序列化得到的String可以唯一的代表一棵树,这个思想很多题都用到了 并不是只是前序遍历就能唯一表示一棵树,加上结构信息就可以了. Map<String,TreeNode> map = new HashMap<>(); List<TreeNode> res = n

[LeetCode] 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 oneof them. Two trees are duplicate if they have the same structure with same node values. Example 1: 1 / 2 3 / / 4

[leetcode-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 oneof them. Two trees are duplicate if they have the same structure with same node values. Example 1: 1 / 2 3 / / 4

LeetCode Problems List 题目汇总

No. Title Level Rate 1 Two Sum Medium 17.70% 2 Add Two Numbers Medium 21.10% 3 Longest Substring Without Repeating Characters Medium 20.60% 4 Median of Two Sorted Arrays Hard 17.40% 5 Longest Palindromic Substring Medium 20.70% 6 ZigZag Conversion Ea

Leetcode problems classified by company 题目按公司分类(Last updated: October 2, 2017)

Sorted by frequency of problems that appear in real interviews.Last updated: October 2, 2017Google (214)534 Design TinyURL388 Longest Absolute File Path683 K Empty Slots340 Longest Substring with At Most K Distinct Characters681 Next Closest Time482

Leetcode 652.寻找重复的子树

寻找重复的子树 给定一棵二叉树,返回所有重复的子树.对于同一类的重复子树,你只需要返回其中任意一棵的根结点即可. 两棵树重复是指它们具有相同的结构以及相同的结点值. 下面是两个重复的子树: 因此,你需要以列表的形式返回上述重复子树的根结点. 思路 Intuition We can serialize each subtree. For example, the tree 1 / 2 3 / 4 5 can be represented as the serialization 1,2,#,#,3

【LeetCode】树(共94题)

[94]Binary Tree Inorder Traversal [95]Unique Binary Search Trees II (2018年11月14日,算法群) 给了一个 n,返回结点是 1 - n 的所有形态的BST. 题解:枚举每个根节点 r, 然后递归的生成左右子树的所有集合,然后做笛卡尔积. 1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *

Aizu 2784 Similarity of Subtrees(树哈希)

Similarity of Subtrees Define the depth of a node in a rooted tree by applying the following rules recursively: The depth of a root node is 0. The depths of child nodes whose parents are with depth d are d+1 . Let S(T,d) be the number of nodes of T w

Oracle 11gR2使用RMAN duplicate复制数据库

11g的RMAN duplicate 个人感觉比10g的先进了很多,10g需在rman备份的基础上进行复制,使用RMAN duplicate创建一个数据完全相同但DBID不同的数据库.而11g的RMAN duplicate 可通过Active database duplicate和Backup-based duplicate两种方法实现.Active database duplicate方式不需要先把目标数据库进行rman备份,只要目标数据库处于归档模式下即可直接通过网络对数据库进行copy,且