描述:递归调用,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[待精简]的更多相关文章

  1. (算法)Binary Tree Max Path Sum

    题目: Given a binary tree, find the maximum path sum. For this problem, a path is defined as any seque ...

  2. #Leet Code# Same Tree

    语言:Python 描述:使用递归实现 # Definition for a binary tree node # class TreeNode: # def __init__(self, x): # ...

  3. #Leet Code# Unique Tree

    语言:Python 描述:使用递归实现 class Solution: # @return an integer def numTrees(self, n): : elif n == : else: ...

  4. 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 思路分析: 题意是将二叉树全部左右子数 ...

  5. [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 ...

  6. 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 ...

  7. 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 ...

  8. 一道算法题目, 二行代码, Binary Tree

    June 8, 2015 我最喜欢的一道算法题目, 二行代码. 编程序需要很强的逻辑思维, 多问几个为什么, 可不可以简化.想一想, 二行代码, 五分钟就可以搞定; 2015年网上大家热议的 Home ...

  9. leetcode : Binary Tree Paths

    Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...

随机推荐

  1. [学习笔记]设计模式之Singleton

    写在前面 为方便读者,本文已添加至索引: 设计模式 魔法手札索引 在前几篇笔记中,我们有了解了部分对象创建型模式,包括Builder(建造者).Abstract Factory(抽象工厂)和Facto ...

  2. 337. House Robber III

    二刷吧..不知道为什么house robbery系列我找不到笔记,不过印象中做了好几次了. 不是很难,用的post-order做bottom-up的运算. 对于一个Node来说,有2种情况,一种是选( ...

  3. Hibernate查询之HQL查询

    转自:http://blog.csdn.net/xiao_yi/article/details/1733342 Criteria查询对查询条件进行了面向对象封装,符合编程人员的思维方式,不过HQL(H ...

  4. swift小结01--基础篇

    2014年推出的swift目前已来到了3.0,日趋稳定,这个是之前学习整理的关于swift的资料,分享给大家,希望有用,写的不足的地方还请见谅. 语言背景:         Swift 语言由苹果公司 ...

  5. oracle10 权限角色

    管理权限和角色 介绍 这一部分我们主要看看oracle中如何管理权限和角色,权限和角色的区别在那里.   当刚刚建立用户时,用户没有任何权限,也不能执行任何操作,oracle数据库会自动创建一个方案, ...

  6. 微信上传图文消息invalid media_id hint,thumb_media_id怎么获取

    微信上传图文消息thumb_media_id, thumb_media_id怎么获取, 微信群发图文消息invalid media_id hint, 微信群发图文消息40007, 40007,inva ...

  7. Asp.net简单三层+Sqllite 增删改查

    新建项目à新建一个空白解决方案 在Model新建一个实体类 using System; using System.Collections.Generic; using System.Linq; usi ...

  8. python 简明教程笔记

    1,python特点 python 注重的是如何解决问题,而不是语法和结构简单高效.扩展性 2,安装 python python -V        检测是否安装pythonctrl+d       ...

  9. 爆牙齿的 Web 标准面试题 【转藏】

    <!DOCTYPE html> <html lang="zh-CN"><head> <meta http-equiv="cont ...

  10. php 半角与全角相关的正则

    半角的全部确认,带小初音的确认法则 ^[ア-ン゙゚ァ-ョッヲー -]+$ <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset// ...