描述:递归调用,getMax返回 [节点值,经过节点左子节点的最大值,经过节点右节点的最大值],每次递归同时查看是否存在不经过节点的值大于max。
代码:待优化
def getLargeNode(self, a, b):
if a and b:
return max(a, b)
elif a and not b:
return a
elif not a and b:
return b
else:
tmp = None def getMax(self, node):
if node is None:
return [None, None, None] left = self.getMax(node.left)
right = self.getMax(node.right) pass_node_max = node.val
if left[0] is not None:
if left[1] > self.maxPath:
self.maxPath = item
if left[2] > self.maxPath:
self.maxPath = item tmp = self.getLargeNode(left[1], left[2]) if tmp is not None:
if tmp <= 0 and left[0] <= 0:
left_val = left[0]
elif tmp > 0 and left[0] <= 0:
left_val = left[0] + tmp
if tmp + left[0] > 0:
pass_node_max += left_val
elif tmp <= 0 and left[0] > 0:
left_val = left[0]
pass_node_max += left_val
else:
left_val = left[0] + tmp
pass_node_max += left_val
else:
left_val = left[0]
if left[0] > 0:
pass_node_max += left[0]
else:
left_val = None if right[0] is not None:
if right[1] > self.maxPath:
self.maxPath = right[1]
if right[2] > self.maxPath:
self.maxPath = right[1] tmp = self.getLargeNode(right[1], right[2]) if tmp is not None:
if tmp <= 0 and right[0] <= 0:
right_val = right[0]
elif tmp > 0 and right[0] <= 0:
right_val = right[0] + tmp
if tmp + right[0] > 0:
pass_node_max += right_val
elif tmp <= 0 and right[0] > 0:
right_val = right[0]
pass_node_max += right_val
else:
right_val = right[0] + tmp
pass_node_max += right_val
else:
right_val = right[0]
if right[0] > 0:
pass_node_max += right[0]
else:
right_val = None if pass_node_max > self.maxPath:
self.maxPath = pass_node_max return [node.val, left_val, right_val] def maxPathSum(self, root):
self.maxPath = root.val
if not(root.left or root.right):
return self.maxPath result = self.getMax(root) root_val = root.val
if result[1] > 0:
root_val += result[1]
if result[2] > 0:
root_val += result[2]
if root_val > self.maxPath:
self.maxPath = root_val if result[1] > self.maxPath:
self.maxPath = result[1]
if result[2] > self.maxPath:
self.maxPath = result[2] return self.maxPath
#Leet Code# Binary Tree Max[待精简]的更多相关文章
-
(算法)Binary Tree Max Path Sum
题目: Given a binary tree, find the maximum path sum. For this problem, a path is defined as any seque ...
-
#Leet Code# Same Tree
语言:Python 描述:使用递归实现 # Definition for a binary tree node # class TreeNode: # def __init__(self, x): # ...
-
#Leet Code# Unique Tree
语言:Python 描述:使用递归实现 class Solution: # @return an integer def numTrees(self, n): : elif n == : else: ...
-
Leet Code OJ 226. Invert Binary Tree [Difficulty: Easy]
题目: Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 思路分析: 题意是将二叉树全部左右子数 ...
-
[Algorithm] Find Max Items and Max Height of a Completely Balanced Binary Tree
A balanced binary tree is something that is used very commonly in analysis of computer science algor ...
-
Cracking the Code Interview 4.3 Array to Binary Tree
Given a sorted (increasing order) array, write an algorithm to create a binary tree with minimal hei ...
-
Google Code Jam 2014 Round 1 A:Problem B. Full Binary Tree
Problem A tree is a connected graph with no cycles. A rooted tree is a tree in which one special ver ...
-
一道算法题目, 二行代码, Binary Tree
June 8, 2015 我最喜欢的一道算法题目, 二行代码. 编程序需要很强的逻辑思维, 多问几个为什么, 可不可以简化.想一想, 二行代码, 五分钟就可以搞定; 2015年网上大家热议的 Home ...
-
leetcode : Binary Tree Paths
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
随机推荐
-
仿淘宝分页按钮效果简单美观易使用的JS分页控件
分页按钮思想: 1.少于9页,全部显示 2.大于9页,1.2页显示,中间页码当前页为中心,前后各留两个页码 附件中有完整例子的压缩包下载.已更新到最新版本 先看效果图: 01输入框焦点效果 ...
- TeeChart注册方法
-
在Oracle中使用rank()over()排名的问题
排序: ---rank()over(order by 列名 排序)的结果是不连续的,如果有4个人,其中有3个是并列第1名,那么最后的排序结果结果如:1 1 1 4 select scoreid, st ...
-
hdu 4911 Inversion(归并排序求逆序对数)2014多校训练第5场
Inversion Time Limit: 20 ...
-
Linux内核中SPI/I2c子系统剖析
Linux内核中,SPI和I2C两个子系统的软件架构是一致的,且Linux内核的驱动模型都以bus,driver,device三种抽象对象为基本元素构建起来.下文的分析将主要用这三种抽象对象的创建过程 ...
-
Android播放音乐时跳动的屏谱demo
Android实现播放音频时的跳动频谱,并实现可以调节的均衡器. Main.java package com.wjq.audiofx; import android.app.Activity; imp ...
-
SpringMVC中通过@ResponseBody返回对象,Js中调用@ResponseBody返回值,统计剩余评论字数的js,@RequestParam默认值,@PathVariable的用法
1.SpringMVC中通过@ResponseBody.@RequestParam默认值,@PathVariable的用法 package com.kuman.cartoon.controller.f ...
-
url加时间戳方法及作用
速记:URL 的末尾追加了时间.这就确保了请求不会在它第一次被发送后即缓存,而是会在此方法每次被调用后重新创建和重发:此 URL 会由于时间戳的不同而稍微有些不同.这种技巧常被用于确保到脚本的 POS ...
-
MFC停靠窗口实现(CDockablePane)
工作中编写MFC界面程序时用到了停靠窗口,为了避免之后用到时再去查询,这里记录下. 步骤 1.定义一个继承自CDockablePane的类 Class CDockableTest : public C ...
-
《Unix&;Linux大学教程》学习笔记6——Unix文件系统
1:Unix文件类型——3种 普通文件(常规文件):文本文件(纯文本.脚本.源程序.配置文件.html等).二进制文件(多媒体文件.数据库等) 目录:用于组织文件 伪文件:不存储数据,目的是提供一种服 ...