国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

LeetCode //C - 124. Binary Tree Maximum Path Sum

這篇具有很好參考價值的文章主要介紹了LeetCode //C - 124. Binary Tree Maximum Path Sum。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

124. Binary Tree Maximum Path Sum

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.

The path sum of a path is the sum of the node’s values in the path.

Given the root of a binary tree, return the maximum path sum of any non-empty path.
?

Example 1:

LeetCode //C - 124. Binary Tree Maximum Path Sum,LeetCode,leetcode,c語言,算法

Input: root = [1,2,3]
Output: 6
Explanation: The optimal path is 2 -> 1 -> 3 with a path sum of 2 + 1 + 3 = 6.

Example 2:

LeetCode //C - 124. Binary Tree Maximum Path Sum,LeetCode,leetcode,c語言,算法

Input: root = [-10,9,20,null,null,15,7]
Output: 42
Explanation: The optimal path is 15 -> 20 -> 7 with a path sum of 15 + 20 + 7 = 42.

Constraints:
  • The number of nodes in the tree is in the range [ 1 , 3 ? 1 0 4 1, 3 * 10^4 1,3?104]
  • -1000 <= Node.val <= 1000

From: LeetCode
Link: 124. Binary Tree Maximum Path Sum


Solution:

Ideas:

Overview:
The problem is to find the maximum path sum in a binary tree. A “path” here means 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.

Approach:
To solve this problem, we perform a post-order traversal of the tree. For each node, we calculate two things:

  1. The maximum path sum considering the current node as an endpoint.
  2. The maximum path sum that could be formed using the current node, which might include paths from its left and/or right child.

The reason we need both values is that while the first one (endpoint value) helps us build the path sum for the parent node, the second value (including the current node) helps us track the global maximum path sum across the tree.

Code Explanation:

  1. helper function: This is a recursive function that traverses the binary tree in a post-order manner. It calculates the maximum path sum for each node and updates the global maximum path sum.

  2. globalMax: This variable keeps track of the maximum path sum encountered so far across the entire tree.

  3. leftMax and rightMax: For each node, we calculate the maximum path sum for its left child and right child.

  4. maxSingle: This represents the maximum path sum considering the current node as an endpoint. This is calculated as the maximum of:

  • The node’s value itself.
  • The node’s value + maximum path sum of the left child.
  • The node’s value + maximum path sum of the right child.
  1. maxTop: This represents the maximum path sum that could be formed using the current node. This is calculated as the maximum of:
  • maxSingle (as explained above).
  • The path sum considering both left and right children + the current node’s value.
  1. globalMax update: For each node, we update the globalMax to be the maximum of the current globalMax and maxTop.

  2. Returning from helper function: We return maxSingle because this represents the maximum value that can be used to form a path sum for the current node’s parent.

  3. maxPathSum function: This function initializes the globalMax to the smallest possible integer value and then calls the helper function to traverse the tree and find the maximum path sum. Finally, it returns the globalMax.文章來源地址http://www.zghlxwxcb.cn/news/detail-705820.html

Code:
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */
int helper(struct TreeNode* root, int* globalMax) {
    if (!root) {
        return 0;
    }

    int leftMax = helper(root->left, globalMax);
    int rightMax = helper(root->right, globalMax);

    int maxSingle = fmax(fmax(leftMax, rightMax) + root->val, root->val);
    int maxTop = fmax(maxSingle, leftMax + rightMax + root->val);

    *globalMax = fmax(*globalMax, maxTop);

    return maxSingle;
}

int maxPathSum(struct TreeNode* root) {
    int globalMax = INT_MIN;
    helper(root, &globalMax);
    return globalMax;
}

到了這里,關(guān)于LeetCode //C - 124. Binary Tree Maximum Path Sum的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進(jìn)行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費用

相關(guān)文章

  • Leetcode 1022. Sum of Root To Leaf Binary Numbers (樹遍歷題)

    Sum of Root To Leaf Binary Numbers Easy 3.3K 183 Companies You are given the root of a binary tree where each node has a value 0 or 1. Each root-to-leaf path represents a binary number starting with the most significant bit. For example, if the path is 0 - 1 - 1 - 0 - 1, then this could represent 01101 in binary, which is 13. For all leaves in the tree, cons

    2024年02月03日
    瀏覽(14)
  • LeetCode //C - 199. Binary Tree Right Side View

    LeetCode //C - 199. Binary Tree Right Side View

    Given the root of a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom. ? Example 1: Input: root = [1,2,3,null,5,null,4] Output: [1,3,4] Example 2: Input: root = [1,null,3] Output: [1,3] Example 3: Input root = [] Output [] Constraints: The number of nodes in the tree is in t

    2024年01月20日
    瀏覽(18)
  • LeetCode //C - 114. Flatten Binary Tree to Linked List

    LeetCode //C - 114. Flatten Binary Tree to Linked List

    Given the root of a binary tree, flatten the tree into a “l(fā)inked list”: The “l(fā)inked list” should use the same TreeNode class where the right child pointer points to the next node in the list and the left child pointer is always null. The “l(fā)inked list” should be in the same order as a pre-order traversal of the binary tree. ? Example 1: Input: ro

    2024年02月09日
    瀏覽(20)
  • Leetcode 1367. Linked List in Binary Tree (二叉樹好題)

    Linked List in Binary Tree Medium Given a binary tree root and a linked list with head as the first node. Return True if all the elements in the linked list starting from the head correspond to some downward path connected in the binary tree otherwise return False. In this context downward path means a path that starts at some node and goes downwards. Exampl

    2024年01月25日
    瀏覽(23)
  • 小白水平理解面試經(jīng)典題目LeetCode 404 Sum of Left Leaves【Tree】

    小白水平理解面試經(jīng)典題目LeetCode 404 Sum of Left Leaves【Tree】

    給定二叉樹的root,返回所有左葉的總和。 葉子是沒有子節(jié)點的節(jié)點。左葉是另一個節(jié)點的左子節(jié)點的葉。 在大學(xué)某個自習(xí)的下午,小白坐在教室看到這道題。想想自己曾經(jīng)和白月光做題,現(xiàn)在大過年的,也是只有自己練題了。左邊一顆樹,右邊一棵樹。。。 這時候黑長直女

    2024年02月22日
    瀏覽(25)
  • Leetcode 3007. Maximum Number That Sum of the Prices Is Less Than or Equal to K

    Leetcode 3007. Maximum Number That Sum of the Prices Is Less Than or Equal to K 1. 解題思路 2. 代碼實現(xiàn) 題目鏈接:3007. Maximum Number That Sum of the Prices Is Less Than or Equal to K 這一題我的思路上就是一個二分的思路,先確定一個上下界,然后不斷通過二分來找到最大的price不超過k的值。 因此,剩下的

    2024年01月20日
    瀏覽(24)
  • LeetCode //C - 106. Construct Binary Tree from Inorder and Postorder Traversal

    LeetCode //C - 106. Construct Binary Tree from Inorder and Postorder Traversal

    Given two integer arrays inorder and postorder where inorder is the inorder traversal of a binary tree and postorder is the postorder traversal of the same tree, construct and return the binary tree. ? Example 1: Input: inorder = [9,3,15,20,7], postorder = [9,15,7,20,3] Output: [3,9,20,null,null,15,7] Example 2: Input: inorder = [-1], postorder = [-1] Outpu

    2024年02月09日
    瀏覽(42)
  • LeetCode //C - 105. Construct Binary Tree from Preorder and Inorder Traversal

    LeetCode //C - 105. Construct Binary Tree from Preorder and Inorder Traversal

    Given two integer arrays preorder and inorder where preorder is the preorder traversal of a binary tree and inorder is the inorder traversal of the same tree, construct and return the binary tree. ? Example 1: Input: preorder = [3,9,20,15,7], inorder = [9,3,15,20,7] Output: [3,9,20,null,null,15,7] Example 2: Input: preorder = [-1], inorder = [-1] Output: [-

    2024年02月09日
    瀏覽(16)
  • leetcode第124場雙周賽

    給你一個整數(shù)數(shù)組? nums ?,如果? nums ? 至少 ?包含? 2 ?個元素,你可以執(zhí)行以下操作: 選擇? nums ?中的前兩個元素并將它們刪除。 一次操作的? 分?jǐn)?shù) ?是被刪除元素的和。 在確保 ?所有操作分?jǐn)?shù)相同 ?的前提下,請你求出? 最多 ?能進(jìn)行多少次操作。 請你返回按照上述

    2024年02月19日
    瀏覽(17)
  • leetcode做題筆記124. 二叉樹中的最大路徑和

    二叉樹中的 ?路徑 ?被定義為一條節(jié)點序列,序列中每對相鄰節(jié)點之間都存在一條邊。同一個節(jié)點在一條路徑序列中? 至多出現(xiàn)一次 ?。該路徑 ?至少包含一個? 節(jié)點,且不一定經(jīng)過根節(jié)點。 路徑和 ?是路徑中各節(jié)點值的總和。 給你一個二叉樹的根節(jié)點? root ?,返回其? 最

    2024年02月10日
    瀏覽(27)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包