#Leet Code# Binary Tree Max[待精简]
描述:递归调用,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 ...
随机推荐
- Unity3D跨平台时partial分部方法的使用
最近看到项目中插件的一部分逻辑,发现问题多多,可读性很差,并且容易出错,于是随手整理了下逻 辑.Unity3D的插件逻辑,因为要考虑到针对各平台的移植,因此会大片的出现#if/#endif等条件编译, ...
- [C#] 常用工具类——直接在浏览器输出数据
/// <summary> /// <para> </para> /// 常用工具类——直接在浏览器输出数据 /// <para> ---------- ...
- 怎样使用SetTimer MFC 够具体
转自:http://blog.csdn.net/ellor/article/details/1714741 Timer事件,即定时器事件,是在游戏编程中,常常使用的一个事件.借助它能够产生定时运行动作 ...
- 在Windows下使用MinGW静态编译Assimp
使用MinGW静态编译Assimp 到了5月份了,没有写一篇日志,于是自己从知识库里面拿出一篇文章充数吧.这次将要解说怎样在Windows下使用MinGW静态编译Assimp. Assimp是眼下比較 ...
- 管理http服务的脚本
因为老是须要又一次安装系统,重一次都要又一次设置http服务的启动脚本.麻烦,所以这一次就把脚本备份出来. httpd for Ubuntu system: nginx + php-fpm #! /b ...
- inux下网络发包工具 Tcpreplay3.x。
第1章. 说明 本文档只适用于Tcpreplay3.x. 第2章. Tcpreplay系列工具 2.1. 概述 首先推荐一个网站:http://tcpreplay.synfin.net ...
- JavaScript 应用开发 #4:切换任务的完成状态
在勾选了任务项目左边的对号(复选框)以后,会将任务的状态标记为已完成,取消勾选的话,又会把任务的状态标记为未完成.所以, 我们需要一个可以切换任务完成状态的方法.在任务模型里面,表示任务状态的属性是 ...
- 购买SSD固态硬盘须当心,你知道什么是SLC、 MLC、TLC闪存芯片颗粒吗?
固态硬盘凭借其存取速率超快等自身优势,被越来越多的电脑爱好者所青睐,并迅速普及到了广大用户的电脑中,因为固态硬盘与传统机械硬盘相比,确实在运行效率等方面有了质的提升,但是亦是美网络小编要提醒大家的是, ...
- Caused by: java.lang.ClassNotFoundException: com/sun/tools/internal/xjc/api/XJC
Caused by: java.lang.ClassNotFoundException: com/sun/tools/internal/xjc/api/XJC 缺少com/sun/tools/inte ...
- Android 5.0 新特性
Material Design Material Design简介 Material Design是谷歌新的设计语言,谷歌希望寄由此来统一各种平台上的用户体验,Material Design的特点是干 ...