Binary Tree Maximum Path Sum
生活随笔
收集整理的這篇文章主要介紹了
Binary Tree Maximum Path Sum
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Binary Tree Maximum Path Sum
題目鏈接:https://leetcode.com/problems...
dfs對每個node,查一下包含這個node的最大路徑值。
/*** Definition for a binary tree node.* public class TreeNode {* int val;* TreeNode left;* TreeNode right;* TreeNode(int x) { val = x; }* }*/ public class Solution {public int maxPathSum(TreeNode root) {dfs(root);return globalMax;}int globalMax = Integer.MIN_VALUE;private int dfs(TreeNode root) {// base caseif(root == null) return 0;int left = Math.max(0, dfs(root.left));int right = Math.max(0, dfs(root.right));globalMax = Math.max(globalMax, left + right + root.val);return Math.max(left, right) + root.val;} }總結
以上是生活随笔為你收集整理的Binary Tree Maximum Path Sum的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Codeforces Round #39
- 下一篇: php 获取js变量