【leetcode】1026. Maximum Difference Between Node and Ancestor
题目如下:
Given the
rootof a binary tree, find the maximum valueVfor which there exists different nodesAandBwhereV = |A.val - B.val|andAis an ancestor ofB.(A node A is an ancestor of B if either: any child of A is equal to B, or any child of A is an ancestor of B.)
Example 1:
Input: [8,3,10,1,6,null,14,null,null,4,7,13]
Output: 7
Explanation:
We have various ancestor-node differences, some of which are given below :
|8 - 3| = 5
|3 - 7| = 4
|8 - 1| = 7
|10 - 13| = 3
Among all possible differences, the maximum value of 7 is obtained by |8 - 1| = 7.Note:
- The number of nodes in the tree is between
2and5000.- Each node will have value between
0and100000.
解题思路:题目要求最大的差值的绝对值。对于任意一个节点来说,其可以得到的最大差值的绝对值只有在这四种情况中:与左子树的最大值,与左子树的最小值,与右子树的最大值,与右子树的最小值。所以只要遍历树,求出每个节点的最大差值,即可得到整棵树的最大差值。当然,为了提高效率,可以缓存每个节点的左子树的最大值、左子树的最小值、右子树的最大值、右子树的最小值。
代码如下:
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution(object):
dic = {} #(max,min)
res = 0
def recursive(self,node,num):
self.dic[num] = (-float('inf'),float('inf'))
if node.left != None:
if num*2 not in self.dic:
self.recursive(node.left, num * 2)
self.res = max(self.res,abs(node.val - self.dic[num*2][0]),abs(node.val - self.dic[num*2][1]))
self.dic[num] = (max(node.val,self.dic[num][0],self.dic[num*2][0]),min(node.val,self.dic[num][1],self.dic[num*2][1])) if node.right != None:
inx = num*2 + 1
if inx not in self.dic:
self.recursive(node.right, num * 2+1)
self.res = max(self.res,abs(node.val - self.dic[inx][0]),abs(node.val - self.dic[inx][1]))
self.dic[num] = (max(node.val,self.dic[num][0],self.dic[inx][0]),min(node.val,self.dic[num][1],self.dic[inx][1])) if node.left == None and node.right == None:
self.dic[num] = (node.val,node.val) def maxAncestorDiff(self, root):
"""
:type root: TreeNode
:rtype: int
"""
self.dic = {}
self.res = 0
self.recursive(root,1)
#print self.dic
return self.res
【leetcode】1026. Maximum Difference Between Node and Ancestor的更多相关文章
- LeetCode 1026. Maximum Difference Between Node and Ancestor
原题链接在这里:https://leetcode.com/problems/maximum-difference-between-node-and-ancestor/ 题目: Given the ro ...
- 【leetcode】998. Maximum Binary Tree II
题目如下: We are given the root node of a maximum tree: a tree where every node has a value greater than ...
- 【LeetCode】662. Maximum Width of Binary Tree 解题报告(Python)
[LeetCode]662. Maximum Width of Binary Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.co ...
- 【LeetCode】895. Maximum Frequency Stack 解题报告(Python)
[LeetCode]895. Maximum Frequency Stack 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxueming ...
- 【LeetCode】718. Maximum Length of Repeated Subarray 解题报告(Python)
[LeetCode]718. Maximum Length of Repeated Subarray 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxu ...
- 【Leetcode】164. Maximum Gap 【基数排序】
Given an unsorted array, find the maximum difference between the successive elements in its sorted f ...
- 【LeetCode】164. Maximum Gap (2 solutions)
Maximum Gap Given an unsorted array, find the maximum difference between the successive elements in ...
- 【LeetCode】1161. Maximum Level Sum of a Binary Tree 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS 日期 题目地址:https://leetcod ...
- 【LeetCode】104. Maximum Depth of Binary Tree 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:BFS 方法二:DFS 参考资料 日期 题目 ...
随机推荐
- 【洛谷P1069 细胞分裂】
题目链接 首先,光看题就觉得它很扯淡(你哪里来这么多的钱来买试管) 根据某位已经ak过ioi的名为ych的神仙说(一看就是数学题,一看就需要因式分解,emm,我果然没有发现美的眼睛qwq) 那么我们就 ...
- 一个包含n个结点的四叉树,每一个节点都有4个指向孩子节点的指针,这4n个指针有(3*n+1)个空指针. 4*n-(n-1) = 3*n+1
因为每个树都有一个头结点.头结点下面是4个子结点,然后每个子结点又有4个子节点.例如一个2层的四叉树,就会有5个结点,但头结点并不能计算进去.他的4个子节点下面接的都是空指针,可以得出空指针的个数为4 ...
- 测开之路一百一十一:bootstrap表单
bootstrap表单 引入bootstrap和jquery 默认表单 垂直表单 表单属性绑定:for属性,当for的属性和id的属性相同时,单击for标签,光标自动跳到相同属性的输入框 复选框 水平 ...
- 【HANA系列】SAP HANA启动出现ERROR
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[HANA系列]SAP HANA启动出现ERRO ...
- pyAudioAnalysis-audioFeatureExtraction 错误纠正
1. TypeError: mfccInitFilterBanks() takes 2 positional arguments but 7 were given The issue In the f ...
- [Python3 填坑] 002 isdecimal() 与 isdigit() 的区别 + isnumeric() 的补充
目录 1. print( 坑的信息 ) 2. isdecimal() 官方文档 3. isdigit() 官方文档 4. 举例 (1) 先说结论 (2) 示例 5. 补充 isnumeric() (1 ...
- HDFS镜像文件fsimage和编辑日志文件edits
镜像文件和编辑日志文件 1)概念 namenode被格式化之后,将在/opt/module/hadoop-2.7.2/data/tmp/dfs/name/current目录中产生如下文件 edits_ ...
- hdu-1281.棋盘游戏(二分图匹配 + 二分图关键点查询)
棋盘游戏 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- [2019杭电多校第四场][hdu6614]AND Minimum Spanning Tree(贪心)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6614 题目大意是有一张n个点的完全图,n个点点权为1-n,边权为两点点权按位与(&).求最小生 ...
- Django中ORM的聚合索引
Django中ORM的聚合索引 在Django中,聚合函数是通过aggregate方法实现的,aggregate方法返回的结果是一个字典 在使用时需要先导入模块from django.db.mod ...
