Leetcode 652. Find Duplicate Subtrees
Given the root
of 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 the same node values.
#include <unordered_map>
#include <vector>
#include <string>
#include <iostream>
// Definition for a binary tree node.
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode() : val(0), left(nullptr), right(nullptr) {}
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};
class Solution {
public:
std::vector<TreeNode*> findDuplicateSubtrees(TreeNode* root) {
std::unordered_map<std::string, int> count;
std::vector<TreeNode*> result;
serialize(root, count, result);
return result;
}
private:
std::string serialize(TreeNode* node, std::unordered_map<std::string, int>& count, std::vector<TreeNode*>& result) {
if (!node)
return "#"; // Use "#" to represent a null node.
// Create a unique serialization for the subtree rooted at `node`.
std::string serial = std::to_string(node->val) + "," +
serialize(node->left, count, result) + "," +
serialize(node->right, count, result);
if (count[serial] == 1)
result.push_back(node);
count[serial]++;
return serial;
}
};
// Helper function to print the tree nodes (for verification purposes)
void printTreeNodeVector(const std::vector<TreeNode*>& nodes) {
for (TreeNode* node : nodes) {
std::cout << node->val << " ";
}
std::cout << std::endl;
}
int main() {
TreeNode* root = new TreeNode(1);
root->left = new TreeNode(2);
root->right = new TreeNode(3);
root->left->left = new TreeNode(4);
root->right->left = new TreeNode(2);
root->right->left->left = new TreeNode(4);
root->right->right = new TreeNode(4);
Solution solution;
std::vector<TreeNode*> duplicates = solution.findDuplicateSubtrees(root);
printTreeNodeVector(duplicates);
return 0;
}
By following this strategy, we can efficiently find all duplicate subtrees within the given binary tree.
Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?