mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-05 16:36:41 +08:00
规范格式
This commit is contained in:
@ -0,0 +1,47 @@
|
||||
package leetcode
|
||||
|
||||
import "math"
|
||||
|
||||
import (
|
||||
"github.com/halfrost/LeetCode-Go/structures"
|
||||
)
|
||||
|
||||
// TreeNode define
|
||||
type TreeNode = structures.TreeNode
|
||||
|
||||
/**
|
||||
* Definition for a binary tree node.
|
||||
* type TreeNode struct {
|
||||
* Val int
|
||||
* Left *TreeNode
|
||||
* Right *TreeNode
|
||||
* }
|
||||
*/
|
||||
|
||||
func maxPathSum(root *TreeNode) int {
|
||||
if root == nil {
|
||||
return 0
|
||||
}
|
||||
max := math.MinInt32
|
||||
getPathSum(root, &max)
|
||||
return max
|
||||
}
|
||||
|
||||
func getPathSum(root *TreeNode, maxSum *int) int {
|
||||
if root == nil {
|
||||
return math.MinInt32
|
||||
}
|
||||
left := getPathSum(root.Left, maxSum)
|
||||
right := getPathSum(root.Right, maxSum)
|
||||
|
||||
currMax := max(max(left+root.Val, right+root.Val), root.Val)
|
||||
*maxSum = max(*maxSum, max(currMax, left+right+root.Val))
|
||||
return currMax
|
||||
}
|
||||
|
||||
func max(a int, b int) int {
|
||||
if a > b {
|
||||
return a
|
||||
}
|
||||
return b
|
||||
}
|
@ -0,0 +1,61 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/halfrost/LeetCode-Go/structures"
|
||||
)
|
||||
|
||||
type question124 struct {
|
||||
para124
|
||||
ans124
|
||||
}
|
||||
|
||||
// para 是参数
|
||||
// one 代表第一个参数
|
||||
type para124 struct {
|
||||
one []int
|
||||
}
|
||||
|
||||
// ans 是答案
|
||||
// one 代表第一个答案
|
||||
type ans124 struct {
|
||||
one int
|
||||
}
|
||||
|
||||
func Test_Problem124(t *testing.T) {
|
||||
|
||||
qs := []question124{
|
||||
|
||||
question124{
|
||||
para124{[]int{}},
|
||||
ans124{0},
|
||||
},
|
||||
|
||||
question124{
|
||||
para124{[]int{1}},
|
||||
ans124{1},
|
||||
},
|
||||
|
||||
question124{
|
||||
para124{[]int{1, 2, 3}},
|
||||
ans124{6},
|
||||
},
|
||||
|
||||
question124{
|
||||
para124{[]int{-10, 9, 20, structures.NULL, structures.NULL, 15, 7}},
|
||||
ans124{42},
|
||||
},
|
||||
}
|
||||
|
||||
fmt.Printf("------------------------Leetcode Problem 124------------------------\n")
|
||||
|
||||
for _, q := range qs {
|
||||
_, p := q.ans124, q.para124
|
||||
fmt.Printf("【input】:%v ", p)
|
||||
root := structures.Ints2TreeNode(p.one)
|
||||
fmt.Printf("【output】:%v \n", maxPathSum(root))
|
||||
}
|
||||
fmt.Printf("\n\n\n")
|
||||
}
|
40
leetcode/0124.Binary-Tree-Maximum-Path-Sum/README.md
Executable file
40
leetcode/0124.Binary-Tree-Maximum-Path-Sum/README.md
Executable file
@ -0,0 +1,40 @@
|
||||
# [124. Binary Tree Maximum Path Sum](https://leetcode.com/problems/binary-tree-maximum-path-sum/)
|
||||
|
||||
|
||||
## 题目
|
||||
|
||||
Given a **non-empty** binary tree, find the maximum path sum.
|
||||
|
||||
For this problem, a path is defined as any sequence of nodes from some starting node to any node in the tree along the parent-child connections. The path must contain **at least one node** and does not need to go through the root.
|
||||
|
||||
**Example 1:**
|
||||
|
||||
Input: [1,2,3]
|
||||
|
||||
1
|
||||
/ \
|
||||
2 3
|
||||
|
||||
Output: 6
|
||||
|
||||
**Example 2:**
|
||||
|
||||
Input: [-10,9,20,null,null,15,7]
|
||||
|
||||
-10
|
||||
/ \
|
||||
9 20
|
||||
/ \
|
||||
15 7
|
||||
|
||||
Output: 42
|
||||
|
||||
## 题目大意
|
||||
|
||||
给定一个非空二叉树,返回其最大路径和。本题中,路径被定义为一条从树中任意节点出发,达到任意节点的序列。该路径至少包含一个节点,且不一定经过根节点。
|
||||
|
||||
## 解题思路
|
||||
|
||||
- 给出一个二叉树,要求找一条路径使得路径的和是最大的。
|
||||
- 这一题思路比较简单,递归维护最大值即可。不过需要比较的对象比较多。`maxPathSum(root) = max(maxPathSum(root.Left), maxPathSum(root.Right), maxPathSumFrom(root.Left) (if>0) + maxPathSumFrom(root.Right) (if>0) + root.Val)` ,其中,`maxPathSumFrom(root) = max(maxPathSumFrom(root.Left), maxPathSumFrom(root.Right)) + root.Val`
|
||||
|
Reference in New Issue
Block a user