Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to analyze the maximum path sum of python binary tree

2025-05-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

Today, I will talk to you about how to analyze the maximum path of python binary tree. Many people may not know much about it. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.

Given a non-empty binary tree, returns its maximum path sum.

In this problem, the path is defined as a sequence that starts from any node in the tree and reaches any node. The path contains at least one node and does not necessarily pass through the root node.

Example 1:

Input: [1Jing 2Jue 3]

one

/\

2 3

Output: 6

Example 2:

Input: [- 10pc9 pr 20pr nullpr nullpr 15pr 7]

-10

/\

9 20

/\

15 7

Output: 42

Ideas for solving the problem:

For the binary tree problem, recursion is preferred, because the subdivision problem is relatively easy, and the maximum path and implicit problem is path continuity.

1, because it may or may not contain roots, so the maximum sum is

Max (the value of root, the left branch contains the maximum sum of roots, the right branch contains the maximum sum of roots, the right branch contains the largest sum of roots, the left branch contains the largest sum of roots and + roots, the left branch contains the maximum sum of roots, the left branch does not contain the maximum sum of roots, and the right branch does not contain the maximum sum of roots)

2, the above problem includes the root (one-sided) maximum sum subproblem, which is solved as

Max (value of root, value of root + maximum sum of left containing root, value of root + maximum sum of right containing root)

Note that it does not include: the value of the maximum sum of roots on the left + the maximum sum of roots on the right, because the route cannot have bifurcations.

The code is as follows

/ * 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} if root.LeftExpressantnil & & root.Rightpowered roomnil {lr:=maxPathSumRoot (root.Left) rr:=maxPathSumRoot (root.Right) l:=maxPathSum (root.Left) r:=maxPathSum (root.Right) AVA = [] int {root.Val,root.Val+lr,root.Val+rr,lr Rr,root.Val+lr+rr,l,r} return max (a)} else if root.Leftfuronil {lr:=maxPathSumRoot (root.Left) l:=maxPathSum (root.Left) aVue = [] int {root.Val,root.Val+lr,lr L} return max (a)} else if root.Rightpowered roomnil {rr:=maxPathSumRoot (root.Right) r:=maxPathSum (root.Right) a root.Right = [] int {root.Val,root.Val+rr,rr,r} return max (a)} return root.Val

}

Func max (arr [] int) int {max:=-1

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report