1339. Maximum Product of Splitted Binary Tree Medium

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



1/**
2 * [1339] Maximum Product of Splitted Binary Tree
3 *
4 * Given the root of a binary tree, split the binary tree into two subtrees by removing one edge such that the product of the sums of the subtrees is maximized.
5 * Return the maximum product of the sums of the two subtrees. Since the answer may be too large, return it modulo 10^9 + 7.
6 * Note that you need to maximize the answer before taking the mod and not after taking it.
7 *  
8 * Example 1:
9 * <img alt="" src="https://assets.leetcode.com/uploads/2020/01/21/sample_1_1699.png" style="width: 500px; height: 167px;" />
10 * Input: root = [1,2,3,4,5,6]
11 * Output: 110
12 * Explanation: Remove the red edge and get 2 binary trees with sum 11 and 10. Their product is 110 (11*10)
13 * 
14 * Example 2:
15 * <img alt="" src="https://assets.leetcode.com/uploads/2020/01/21/sample_2_1699.png" style="width: 500px; height: 211px;" />
16 * Input: root = [1,null,2,3,4,null,null,5,6]
17 * Output: 90
18 * Explanation: Remove the red edge and get 2 binary trees with sum 15 and 6.Their product is 90 (15*6)
19 * 
20 *  
21 * Constraints:
22 * 
23 * 	The number of nodes in the tree is in the range [2, 5 * 10^4].
24 * 	1 <= Node.val <= 10^4
25 * 
26 */
27pub struct Solution {}
28use crate::util::tree::{TreeNode, to_tree};
29
30// problem: https://leetcode.com/problems/maximum-product-of-splitted-binary-tree/
31// discuss: https://leetcode.com/problems/maximum-product-of-splitted-binary-tree/discuss/?currentPage=1&orderBy=most_votes&query=
32
33// submission codes start here
34
35// Definition for a binary tree node.
36// #[derive(Debug, PartialEq, Eq)]
37// pub struct TreeNode {
38//   pub val: i32,
39//   pub left: Option<Rc<RefCell<TreeNode>>>,
40//   pub right: Option<Rc<RefCell<TreeNode>>>,
41// }
42// 
43// impl TreeNode {
44//   #[inline]
45//   pub fn new(val: i32) -> Self {
46//     TreeNode {
47//       val,
48//       left: None,
49//       right: None
50//     }
51//   }
52// }
53use std::rc::Rc;
54use std::cell::RefCell;
55impl Solution {
56    pub fn max_product(root: Option<Rc<RefCell<TreeNode>>>) -> i32 {
57        0
58    }
59}
60
61// submission codes end
62
63#[cfg(test)]
64mod tests {
65    use super::*;
66
67    #[test]
68    fn test_1339() {
69    }
70}
71


Back
© 2025 bowen.ge All Rights Reserved.