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.
給一個非空二元樹,找到最大路徑和。
在這個問題中,一段路徑定義為任何節點序列,其開始節點為樹中的任一節點,且前後節點的關係都是父子關係的連接。這段路徑至少有包含一個節點,且不一定要經過樹根。
n
並將n
當作樹根的最大路徑和。接著跑每一個點找出最大值即可。max(0, XXX)
來處理。max(左邊最大, 又邊最大)
)。LeetCode
C++