新博文地址:[leetcode]Binary Tree Maximum Path Sum
Binary Tree Maximum Path Sum
Given a binary tree, find the maximum path sum.
The path may start and end at any node in the tree.
For example:
Given the below binary tree,
1
/ \
2 3
Return 6.
The path may start and end at any node in the tree.
For example:
Given the below binary tree,
1
/ \
2 3
Return 6.
这道题是非常好的一道题,树,常规做法是遍历,遍历的做法往往用dfs,代码简单,可读性强。这道题与常规的树的题目不同的一点是,在遍历的过程中,进行了返回处理,而且做法很巧妙。
以这棵树为例:
2
/ \
1 -1
/ \ / \
1 -3 1 7
/ \
1 -1
/ \ / \
1 -3 1 7
图中红色路径表示的一条最大路径:
先看下递归过程:
以左下角的1为root的树的最大路径肯定是1,而异-3为root的树的最大路径是-3,以他俩的父节点1为根节点的最大路径有这么几种情况:
root.val, root.val + left , root.val + right ,root.val + left + right
取其中的最大值(图中的例子是root.val + left:2),更新max(上面取四个值得最大值,仅用于更新max)
但是这个递归函数的返回不应该包括root.val + left + right,因为黄底色的1作为2的左子树的返回值只能由其他节点经过黄底色的1,传到根节点2,单向,传到2,不能形成折路(谁想到的?太厉害了)
好了有了以上的算法思想,代码实现起来就比较容易了:
int max = Integer.MIN_VALUE; public int maxPathSum(TreeNode root){ if(root == null) return 0; dfs(root); return max; } public int dfs(TreeNode root){ if(root == null) return 0; int left = dfs(root.left); int right = dfs(root.right); int tem = Math.max(root.val, Math.max(left+ root.val, Math.max(right + root.val, root.val + left+ right))); max = tem > max ? tem : max; return Math.max(root.val, Math.max(root.val + left, root.val + right)); }
相关推荐
主要介绍了LeetCode -- Path Sum III分析及实现方法的相关资料,希望通过本文能帮助到大家,需要的朋友可以参考下
leetcode的题目:Balanced Binary Tree
双指针算法,python数组双指针算法求和问题LeetCode2sum3sum4sum含代码
文档python数组双指针算法求和问题LeetCode2sum3sum4sum含代码提取方式是百度网盘分享地址
答案LeetCode_1_TwoSum LeetCode 问题:给定一个整数数组,找出两个数字,使它们相加为特定的目标数字。 函数 twoSum 应该返回两个数字的索引,使它们相加为目标,其中 index1 必须小于 index2。 请注意,您返回的...
二叉树前序遍历,leetcode
Binary Tree Maximum Path Sum Populating Next Right Pointers in Each Node Sum Root to Leaf Numbers LCA of Binary Tree 线段树 Range Sum Query - Mutable 排序 插入排序 Insertion Sort List 归并排序 Merge ...
BinaryTree.py是一个方便的工具,它可以构建和显示编码时所需的二叉树 演示 您需要在使用之前导入该类 import BinaryTree as bt 从值/对象列表或二叉树构造二叉树 t1 = bt.BinaryTree([1,2,3,4,5,'#',6,7,'#','#','#...
144.Binary_Tree_Preorder_Traversal二叉树的前序遍历【LeetCode单题讲解系列】
145.Binary_Tree_Postorder_Traversal二叉树的后序遍历【LeetCode单题讲解系列】
Source file for LeetCode Permutations Problem
94.Binary_Tree_Inorder_Traversal二叉树的中序遍历【LeetCode单题讲解系列】
我的个人微信公众号:Microstrong 微信公众号ID:MicrostrongAI 微信公众号介绍:Microstrong(小强)同学主要研究机器学习、深度学习、计算机视觉、智能对话系统相关内容,分享在学习过程中的...102. Binary Tree Leve
leetcode 树节点二叉树最大路径和 执行 : /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val ...
Given a binary tree and a sum, find all root-to-leaf paths where each path’s sum equals the given sum. Note: A leaf is a node with no children. Example: Given the below binary tree and sum = 22, ...
LeetCode — Path Sum III分析及实现方法 题目描述: You are given a binary tree in which each node contains an integer value. Find the number of paths that sum to a given value. The path does not need...
leetcode-tag-Tree
Leetcode-tree 94题 Binary Tree Inorder Traversal 对于树的问题,大多数我们都会使用递归的方法。原因是树的左子树也是树,右子树也是树,使用递归的方法最简单快捷。这道题是需要我们用Inorder的顺序输出节点。in...
144-Binary Tree Preorder Traversal94-Binary Tree Inorder Traversal145-Binary Tree Postorder Traversal(后续的非递归时间不够可以先跳过,有点难)层次遍历,队列辅助,相当于广搜。102-Binary Tree Level ...
* [Binary Search Tree](https://github.com/kamyu104/LeetCode#binary-search-tree) * [Breadth-First Search](https://github.com/kamyu104/LeetCode#breadth-first-search) * [Depth-First Search]...