208 - 124 二叉树中的最大路径和
题目
给定一个非空二叉树,返回其最大路径和。
本题中,路径被定义为一条从树中任意节点出发,达到任意节点的序列。该路径至少包含一个节点,且不一定经过根节点
示例 1:
输入: [1,2,3]
输出: 6
示例 2:
输入: [-10,9,20,null,null,15,7]
-10 / 9 20 / 15 7
输出: 42
解答
递归
计算左子树、右子树,加上当前节点的值
Runtime: 88 ms, faster than 75.56% of Python3 online submissions for Binary Tree Maximum Path Sum.
Memory Usage: 20.2 MB, less than 100.00% of Python3 online submissions for Binary Tree Maximum Path Sum.
Runtime: 72 ms, faster than 99.59% of Python3 online submissions for Binary Tree Maximum Path Sum.
Memory Usage: 20.2 MB, less than 100.00% of Python3 online submissions for Binary Tree Maximum Path Sum.
光头大佬版本。。
Runtime: 92 ms, faster than 56.23% of Python3 online submissions for Binary Tree Maximum Path Sum.
Memory Usage: 20.3 MB, less than 100.00% of Python3 online submissions for Binary Tree Maximum Path Sum.
Last updated
Was this helpful?