124. Binary Tree Maximum Path Sum Hard
1/**
2 * [124] Binary Tree Maximum Path Sum
3 *
4 * A path in a binary tree is a sequence of nodes where each pair of adjacent nodes in the sequence has an edge connecting them. A node can only appear in the sequence at most once. Note that the path does not need to pass through the root.
5 * The path sum of a path is the sum of the node's values in the path.
6 * Given the root of a binary tree, return the maximum path sum of any non-empty path.
7 *
8 * Example 1:
9 * <img alt="" src="https://assets.leetcode.com/uploads/2020/10/13/exx1.jpg" style="width: 322px; height: 182px;" />
10 * Input: root = [1,2,3]
11 * Output: 6
12 * Explanation: The optimal path is 2 -> 1 -> 3 with a path sum of 2 + 1 + 3 = 6.
13 *
14 * Example 2:
15 * <img alt="" src="https://assets.leetcode.com/uploads/2020/10/13/exx2.jpg" />
16 * Input: root = [-10,9,20,null,null,15,7]
17 * Output: 42
18 * Explanation: The optimal path is 15 -> 20 -> 7 with a path sum of 15 + 20 + 7 = 42.
19 *
20 *
21 * Constraints:
22 *
23 * The number of nodes in the tree is in the range [1, 3 * 10^4].
24 * -1000 <= Node.val <= 1000
25 *
26 */
27pub struct Solution {}
28use crate::util::tree::{TreeNode, to_tree};
29
30// problem: https://leetcode.com/problems/binary-tree-maximum-path-sum/
31// discuss: https://leetcode.com/problems/binary-tree-maximum-path-sum/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_path_sum(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_124() {
69 }
70}
71
Back
© 2025 bowen.ge All Rights Reserved.