652. Find Duplicate Subtrees Medium

@problem@discussion
#Hash Table#Tree#Depth-First Search#Binary Tree



1/**
2 * [652] Find Duplicate Subtrees
3 *
4 * Given the root of a binary tree, return all duplicate subtrees.
5 * For each kind of duplicate subtrees, you only need to return the root node of any one of them.
6 * Two trees are duplicate if they have the same structure with the same node values.
7 *  
8 * Example 1:
9 * <img alt="" src="https://assets.leetcode.com/uploads/2020/08/16/e1.jpg" style="width: 450px; height: 354px;" />
10 * Input: root = [1,2,3,4,null,2,4,null,null,4]
11 * Output: [[2,4],[4]]
12 * 
13 * Example 2:
14 * <img alt="" src="https://assets.leetcode.com/uploads/2020/08/16/e2.jpg" style="width: 321px; height: 201px;" />
15 * Input: root = [2,1,1]
16 * Output: [[1]]
17 * 
18 * Example 3:
19 * <img alt="" src="https://assets.leetcode.com/uploads/2020/08/16/e33.jpg" style="width: 450px; height: 303px;" />
20 * Input: root = [2,2,2,3,null,3,null]
21 * Output: [[2,3],[3]]
22 * 
23 *  
24 * Constraints:
25 * 
26 * 	The number of the nodes in the tree will be in the range [1, 5000]
27 * 	-200 <= Node.val <= 200
28 * 
29 */
30pub struct Solution {}
31use crate::util::tree::{TreeNode, to_tree};
32
33// problem: https://leetcode.com/problems/find-duplicate-subtrees/
34// discuss: https://leetcode.com/problems/find-duplicate-subtrees/discuss/?currentPage=1&orderBy=most_votes&query=
35
36// submission codes start here
37
38// Definition for a binary tree node.
39// #[derive(Debug, PartialEq, Eq)]
40// pub struct TreeNode {
41//   pub val: i32,
42//   pub left: Option<Rc<RefCell<TreeNode>>>,
43//   pub right: Option<Rc<RefCell<TreeNode>>>,
44// }
45// 
46// impl TreeNode {
47//   #[inline]
48//   pub fn new(val: i32) -> Self {
49//     TreeNode {
50//       val,
51//       left: None,
52//       right: None
53//     }
54//   }
55// }
56use std::rc::Rc;
57use std::cell::RefCell;
58impl Solution {
59    pub fn find_duplicate_subtrees(root: Option<Rc<RefCell<TreeNode>>>) -> Vec<Option<Rc<RefCell<TreeNode>>>> {
60        vec![]
61    }
62}
63
64// submission codes end
65
66#[cfg(test)]
67mod tests {
68    use super::*;
69
70    #[test]
71    fn test_652() {
72    }
73}
74


Back
© 2025 bowen.ge All Rights Reserved.