topic124

package
v0.0.0-...-b071cee Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 9, 2023 License: GPL-3.0 Imports: 1 Imported by: 0

README

二叉树中的最大路径和

1. 题目描述

路径 被定义为一条从树中任意节点出发,沿父节点-子节点连接,达到任意节点的序列。同一个节点在一条路径序列中 至多出现一次 。该路径 至少包含一个 节点,且不一定经过根节点。

路径和 是路径中各节点值的总和。

给你一个二叉树的根节点 root ,返回其 最大路径和 。

2. 示例

示例1 1

输入:root = [1,2,3]
输出:6
解释:最优路径是 2 -> 1 -> 3 ,路径和为 2 + 1 + 3 = 6

示例2 2

输入:root = [-10,9,20,null,null,15,7]
输出:42
解释:最优路径是 15 -> 20 -> 7 ,路径和为 15 + 20 + 7 = 42

提示

  • 树中节点数目范围是 [1, 3 * $10^4$]
  • -1000 <= Node.val <= 1000

3. 解题

  • 使用递归,递归函数功能为求某一树的从根开始的最大路径和
  • 则本题所要求的最大路径和即为:某一节点的值+其左子树的从根开始的最大路径和+其右子树的从根开始的最大路径和

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type TreeNode

type TreeNode struct {
	Val   int
	Left  *TreeNode
	Right *TreeNode
}

func BuildTree

func BuildTree(preorder []int, inorder []int) *TreeNode

func (*TreeNode) InOrder

func (root *TreeNode) InOrder(Visit func(val int))

func (*TreeNode) PostOrder

func (root *TreeNode) PostOrder(Visit func(val int))

func (*TreeNode) PreOrder

func (root *TreeNode) PreOrder(Visit func(val int))

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL