508. Most Frequent Subtree Sum Medium

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



1/**
2 * [508] Most Frequent Subtree Sum
3 *
4 * Given the root of a binary tree, return the most frequent subtree sum. If there is a tie, return all the values with the highest frequency in any order.
5 * The subtree sum of a node is defined as the sum of all the node values formed by the subtree rooted at that node (including the node itself).
6 *  
7 * Example 1:
8 * <img alt="" src="https://assets.leetcode.com/uploads/2021/04/24/freq1-tree.jpg" style="width: 207px; height: 183px;" />
9 * Input: root = [5,2,-3]
10 * Output: [2,-3,4]
11 * 
12 * Example 2:
13 * <img alt="" src="https://assets.leetcode.com/uploads/2021/04/24/freq2-tree.jpg" style="width: 207px; height: 183px;" />
14 * Input: root = [5,2,-5]
15 * Output: [2]
16 * 
17 *  
18 * Constraints:
19 * 
20 * 	The number of nodes in the tree is in the range [1, 10^4].
21 * 	-10^5 <= Node.val <= 10^5
22 * 
23 */
24pub struct Solution {}
25use crate::util::tree::{TreeNode, to_tree};
26
27// problem: https://leetcode.com/problems/most-frequent-subtree-sum/
28// discuss: https://leetcode.com/problems/most-frequent-subtree-sum/discuss/?currentPage=1&orderBy=most_votes&query=
29
30// submission codes start here
31
32// Definition for a binary tree node.
33// #[derive(Debug, PartialEq, Eq)]
34// pub struct TreeNode {
35//   pub val: i32,
36//   pub left: Option<Rc<RefCell<TreeNode>>>,
37//   pub right: Option<Rc<RefCell<TreeNode>>>,
38// }
39// 
40// impl TreeNode {
41//   #[inline]
42//   pub fn new(val: i32) -> Self {
43//     TreeNode {
44//       val,
45//       left: None,
46//       right: None
47//     }
48//   }
49// }
50use std::rc::Rc;
51use std::cell::RefCell;
52impl Solution {
53    pub fn find_frequent_tree_sum(root: Option<Rc<RefCell<TreeNode>>>) -> Vec<i32> {
54        vec![]
55    }
56}
57
58// submission codes end
59
60#[cfg(test)]
61mod tests {
62    use super::*;
63
64    #[test]
65    fn test_508() {
66    }
67}
68


Back
© 2025 bowen.ge All Rights Reserved.