diff --git a/solution/1300-1399/1339.Maximum Product of Splitted Binary Tree/README.md b/solution/1300-1399/1339.Maximum Product of Splitted Binary Tree/README.md index fe7e6975124a9..3f21ac2eb066c 100644 --- a/solution/1300-1399/1339.Maximum Product of Splitted Binary Tree/README.md +++ b/solution/1300-1399/1339.Maximum Product of Splitted Binary Tree/README.md @@ -75,9 +75,9 @@ tags: 我们可以用两次 DFS 来解决这个问题。 -第一次,我们用一个 $sum(root)$ 函数递归求出整棵树所有节点的和,记为 $s$。 +第一次,我们用一个 $\text{sum}(\text{root})$ 函数递归求出整棵树所有节点的和,记为 $s$。 -第二次,我们用一个 $dfs(root)$ 函数递归遍历每个节点,求出以当前节点为根的子树的节点和 $t$,那么当前节点与其父节点分裂后两棵子树的节点和分别为 $t$ 和 $s - t$,它们的乘积为 $t \times (s - t)$,我们遍历所有节点,求出乘积的最大值,即为答案。 +第二次,我们用一个 $\text{dfs}(\text{root})$ 函数递归遍历每个节点,求出以当前节点为根的子树的节点和 $t$,那么当前节点与其父节点分裂后两棵子树的节点和分别为 $t$ 和 $s - t$,它们的乘积为 $t \times (s - t)$,我们遍历所有节点,求出乘积的最大值,即为答案。 时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是二叉树的节点数。 @@ -185,7 +185,7 @@ public: ll ans = 0; const int mod = 1e9 + 7; - function sum = [&](TreeNode* root) -> ll { + auto sum = [&](this auto&& sum, TreeNode* root) -> ll { if (!root) { return 0; } @@ -194,7 +194,7 @@ public: ll s = sum(root); - function dfs = [&](TreeNode* root) -> ll { + auto dfs = [&](this auto&& dfs, TreeNode* root) -> ll { if (!root) { return 0; } @@ -291,6 +291,63 @@ function maxProduct(root: TreeNode | null): number { } ``` +#### Rust + +```rust +// Definition for a binary tree node. +// #[derive(Debug, PartialEq, Eq)] +// pub struct TreeNode { +// pub val: i32, +// pub left: Option>>, +// pub right: Option>>, +// } +// +// impl TreeNode { +// #[inline] +// pub fn new(val: i32) -> Self { +// TreeNode { +// val, +// left: None, +// right: None +// } +// } +// } +use std::rc::Rc; +use std::cell::RefCell; + +impl Solution { + pub fn max_product(root: Option>>) -> i32 { + const MOD: i64 = 1_000_000_007; + let mut ans: i64 = 0; + let s = Self::sum(&root); + Self::dfs(&root, s, &mut ans); + (ans % MOD) as i32 + } + + fn dfs(root: &Option>>, s: i64, ans: &mut i64) -> i64 { + if root.is_none() { + return 0; + } + let node = root.as_ref().unwrap().borrow(); + let t = node.val as i64 + + Self::dfs(&node.left, s, ans) + + Self::dfs(&node.right, s, ans); + if t < s { + *ans = (*ans).max(t * (s - t)); + } + t + } + + fn sum(root: &Option>>) -> i64 { + if root.is_none() { + return 0; + } + let node = root.as_ref().unwrap().borrow(); + node.val as i64 + Self::sum(&node.left) + Self::sum(&node.right) + } +} +``` + diff --git a/solution/1300-1399/1339.Maximum Product of Splitted Binary Tree/README_EN.md b/solution/1300-1399/1339.Maximum Product of Splitted Binary Tree/README_EN.md index ab9e0eb8e5195..d442987b602cf 100644 --- a/solution/1300-1399/1339.Maximum Product of Splitted Binary Tree/README_EN.md +++ b/solution/1300-1399/1339.Maximum Product of Splitted Binary Tree/README_EN.md @@ -57,7 +57,15 @@ tags: -### Solution 1 +### Solution 1: Two DFS + +We can solve this problem with two DFS traversals. + +In the first traversal, we use a $\text{sum}(\text{root})$ function to recursively calculate the sum of all nodes in the entire tree, denoted as $s$. + +In the second traversal, we use a $\text{dfs}(\text{root})$ function to recursively traverse each node and calculate the sum of nodes in the subtree rooted at the current node, denoted as $t$. After splitting at the current node and its parent, the sums of the two subtrees are $t$ and $s - t$ respectively, and their product is $t \times (s - t)$. We traverse all nodes to find the maximum product, which is the answer. + +The time complexity is $O(n)$, and the space complexity is $O(n)$, where $n$ is the number of nodes in the binary tree. @@ -163,7 +171,7 @@ public: ll ans = 0; const int mod = 1e9 + 7; - function sum = [&](TreeNode* root) -> ll { + auto sum = [&](this auto&& sum, TreeNode* root) -> ll { if (!root) { return 0; } @@ -172,7 +180,7 @@ public: ll s = sum(root); - function dfs = [&](TreeNode* root) -> ll { + auto dfs = [&](this auto&& dfs, TreeNode* root) -> ll { if (!root) { return 0; } @@ -269,6 +277,63 @@ function maxProduct(root: TreeNode | null): number { } ``` +#### Rust + +```rust +// Definition for a binary tree node. +// #[derive(Debug, PartialEq, Eq)] +// pub struct TreeNode { +// pub val: i32, +// pub left: Option>>, +// pub right: Option>>, +// } +// +// impl TreeNode { +// #[inline] +// pub fn new(val: i32) -> Self { +// TreeNode { +// val, +// left: None, +// right: None +// } +// } +// } +use std::rc::Rc; +use std::cell::RefCell; + +impl Solution { + pub fn max_product(root: Option>>) -> i32 { + const MOD: i64 = 1_000_000_007; + let mut ans: i64 = 0; + let s = Self::sum(&root); + Self::dfs(&root, s, &mut ans); + (ans % MOD) as i32 + } + + fn dfs(root: &Option>>, s: i64, ans: &mut i64) -> i64 { + if root.is_none() { + return 0; + } + let node = root.as_ref().unwrap().borrow(); + let t = node.val as i64 + + Self::dfs(&node.left, s, ans) + + Self::dfs(&node.right, s, ans); + if t < s { + *ans = (*ans).max(t * (s - t)); + } + t + } + + fn sum(root: &Option>>) -> i64 { + if root.is_none() { + return 0; + } + let node = root.as_ref().unwrap().borrow(); + node.val as i64 + Self::sum(&node.left) + Self::sum(&node.right) + } +} +``` + diff --git a/solution/1300-1399/1339.Maximum Product of Splitted Binary Tree/Solution.cpp b/solution/1300-1399/1339.Maximum Product of Splitted Binary Tree/Solution.cpp index 8070415edce0c..6b31eee0e58f8 100644 --- a/solution/1300-1399/1339.Maximum Product of Splitted Binary Tree/Solution.cpp +++ b/solution/1300-1399/1339.Maximum Product of Splitted Binary Tree/Solution.cpp @@ -16,7 +16,7 @@ class Solution { ll ans = 0; const int mod = 1e9 + 7; - function sum = [&](TreeNode* root) -> ll { + auto sum = [&](this auto&& sum, TreeNode* root) -> ll { if (!root) { return 0; } @@ -25,7 +25,7 @@ class Solution { ll s = sum(root); - function dfs = [&](TreeNode* root) -> ll { + auto dfs = [&](this auto&& dfs, TreeNode* root) -> ll { if (!root) { return 0; } @@ -39,4 +39,4 @@ class Solution { dfs(root); return ans % mod; } -}; \ No newline at end of file +}; diff --git a/solution/1300-1399/1339.Maximum Product of Splitted Binary Tree/Solution.rs b/solution/1300-1399/1339.Maximum Product of Splitted Binary Tree/Solution.rs new file mode 100644 index 0000000000000..7f8a1d3bd6b56 --- /dev/null +++ b/solution/1300-1399/1339.Maximum Product of Splitted Binary Tree/Solution.rs @@ -0,0 +1,52 @@ +// Definition for a binary tree node. +// #[derive(Debug, PartialEq, Eq)] +// pub struct TreeNode { +// pub val: i32, +// pub left: Option>>, +// pub right: Option>>, +// } +// +// impl TreeNode { +// #[inline] +// pub fn new(val: i32) -> Self { +// TreeNode { +// val, +// left: None, +// right: None +// } +// } +// } +use std::rc::Rc; +use std::cell::RefCell; + +impl Solution { + pub fn max_product(root: Option>>) -> i32 { + const MOD: i64 = 1_000_000_007; + let mut ans: i64 = 0; + let s = Self::sum(&root); + Self::dfs(&root, s, &mut ans); + (ans % MOD) as i32 + } + + fn dfs(root: &Option>>, s: i64, ans: &mut i64) -> i64 { + if root.is_none() { + return 0; + } + let node = root.as_ref().unwrap().borrow(); + let t = node.val as i64 + + Self::dfs(&node.left, s, ans) + + Self::dfs(&node.right, s, ans); + if t < s { + *ans = (*ans).max(t * (s - t)); + } + t + } + + fn sum(root: &Option>>) -> i64 { + if root.is_none() { + return 0; + } + let node = root.as_ref().unwrap().borrow(); + node.val as i64 + Self::sum(&node.left) + Self::sum(&node.right) + } +}