652. Find Duplicate Subtrees
Posted jtechroad
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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; } };
以上是关于652. Find Duplicate Subtrees的主要内容,如果未能解决你的问题,请参考以下文章
LeetCode 652. Find Duplicate Subtrees
[Leetcode]652.Find Duplicate Subtrees
[LeetCode]652. Find Duplicate Subtrees找到重复树
287. Find the Duplicate Number