Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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$ 是二叉树的节点数。

Expand Down Expand Up @@ -185,7 +185,7 @@ public:
ll ans = 0;
const int mod = 1e9 + 7;

function<ll(TreeNode*)> sum = [&](TreeNode* root) -> ll {
auto sum = [&](this auto&& sum, TreeNode* root) -> ll {
if (!root) {
return 0;
}
Expand All @@ -194,7 +194,7 @@ public:

ll s = sum(root);

function<ll(TreeNode*)> dfs = [&](TreeNode* root) -> ll {
auto dfs = [&](this auto&& dfs, TreeNode* root) -> ll {
if (!root) {
return 0;
}
Expand Down Expand Up @@ -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<Rc<RefCell<TreeNode>>>,
// pub right: Option<Rc<RefCell<TreeNode>>>,
// }
//
// 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<Rc<RefCell<TreeNode>>>) -> 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<Rc<RefCell<TreeNode>>>, 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<Rc<RefCell<TreeNode>>>) -> 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)
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,15 @@ tags:

<!-- solution:start -->

### 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.

<!-- tabs:start -->

Expand Down Expand Up @@ -163,7 +171,7 @@ public:
ll ans = 0;
const int mod = 1e9 + 7;

function<ll(TreeNode*)> sum = [&](TreeNode* root) -> ll {
auto sum = [&](this auto&& sum, TreeNode* root) -> ll {
if (!root) {
return 0;
}
Expand All @@ -172,7 +180,7 @@ public:

ll s = sum(root);

function<ll(TreeNode*)> dfs = [&](TreeNode* root) -> ll {
auto dfs = [&](this auto&& dfs, TreeNode* root) -> ll {
if (!root) {
return 0;
}
Expand Down Expand Up @@ -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<Rc<RefCell<TreeNode>>>,
// pub right: Option<Rc<RefCell<TreeNode>>>,
// }
//
// 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<Rc<RefCell<TreeNode>>>) -> 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<Rc<RefCell<TreeNode>>>, 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<Rc<RefCell<TreeNode>>>) -> 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)
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Solution {
ll ans = 0;
const int mod = 1e9 + 7;

function<ll(TreeNode*)> sum = [&](TreeNode* root) -> ll {
auto sum = [&](this auto&& sum, TreeNode* root) -> ll {
if (!root) {
return 0;
}
Expand All @@ -25,7 +25,7 @@ class Solution {

ll s = sum(root);

function<ll(TreeNode*)> dfs = [&](TreeNode* root) -> ll {
auto dfs = [&](this auto&& dfs, TreeNode* root) -> ll {
if (!root) {
return 0;
}
Expand All @@ -39,4 +39,4 @@ class Solution {
dfs(root);
return ans % mod;
}
};
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Definition for a binary tree node.
// #[derive(Debug, PartialEq, Eq)]
// pub struct TreeNode {
// pub val: i32,
// pub left: Option<Rc<RefCell<TreeNode>>>,
// pub right: Option<Rc<RefCell<TreeNode>>>,
// }
//
// 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<Rc<RefCell<TreeNode>>>) -> 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<Rc<RefCell<TreeNode>>>, 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<Rc<RefCell<TreeNode>>>) -> 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)
}
}