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.

 

Example 1:

Input: root = [1,2,3,4,null,2,4,null,null,4]
Output: [[2,4],[4]]

Example 2:

Input: root = [2,1,1]
Output: [[1]]

Example 3:

Input: root = [2,2,2,3,null,3,null]
Output: [[2,3],[3]]

 

Constraints:

  • The number of the nodes in the tree will be in the range [1, 10^4]
  • -200 <= Node.val <= 200

Rust Solution

struct Solution;
use rustgym_util::*;
use std::collections::hash_map::DefaultHasher;
use std::collections::HashMap;
use std::hash::Hasher;

trait Preorder {
    fn preorder(&self, all: &mut Vec<TreeLink>, count: &mut HashMap<u64, usize>);
    fn hash<H: Hasher>(&self, hasher: &mut H);
}

impl Preorder for TreeLink {
    fn preorder(&self, all: &mut Vec<TreeLink>, count: &mut HashMap<u64, usize>) {
        if self.is_some() {
            let mut hasher = DefaultHasher::new();
            self.hash(&mut hasher);
            let hash = hasher.finish();
            *count.entry(hash).or_default() += 1;
            if count[&hash] == 2 {
                all.push(self.clone())
            }
        }
        if let Some(node) = self {
            let node = node.borrow();
            node.left.preorder(all, count);
            node.right.preorder(all, count);
        }
    }

    fn hash<H: Hasher>(&self, hasher: &mut H) {
        if let Some(node) = self {
            let node = node.borrow();
            hasher.write_i32(node.val);
            node.left.hash(hasher);
            node.right.hash(hasher);
        } else {
            hasher.write_i32(std::i32::MAX);
        }
    }
}

impl Solution {
    fn find_duplicate_subtrees(root: TreeLink) -> Vec<TreeLink> {
        let mut res: Vec<TreeLink> = vec![];
        let mut count: HashMap<u64, usize> = HashMap::new();
        root.preorder(&mut res, &mut count);
        res
    }
}

#[test]
fn test() {
    let root = tree!(
        1,
        tree!(2, tree!(4), None),
        tree!(3, tree!(2, tree!(4), None), tree!(4))
    );
    let mut res = vec![tree!(2, tree!(4), None), tree!(4)];
    let mut ans = Solution::find_duplicate_subtrees(root);
    res.sort();
    ans.sort();
    assert_eq!(ans, res);
    let root = tree!(
        0,
        tree!(0, tree!(0), None),
        tree!(0, None, tree!(0, None, tree!(0)))
    );
    let mut res = vec![tree!(0)];
    let mut ans = Solution::find_duplicate_subtrees(root);
    res.sort();
    ans.sort();
    assert_eq!(ans, res);
}

Having problems with this solution? Click here to submit an issue on github.