【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 参考资料 日期 题目 ...
随机推荐
- Django项目执行时No Module Named ' ' 问题的解决情况
出现这种问题的情况大致都是因为该模块未安装,使用 pip install xxx 进行安装,即可解决此类问题. 出现ModuleNotFoundError: No module named 'rest ...
- VxWorks BSP开发入门
VxWorks将所有硬件平台相关的代码封装在BSP(Board Support Package)库中,从而为应用层代码提供了独立于硬件平台和体系接口的特性. BSP库中封装了一组统一的API,包括硬件 ...
- JS new date在IOS出现的问题
实例代码: input = input.replace(/\-/g, "/");//横杠的时间不能被识别,所以要替换程斜杠 let time = new Date(input); ...
- 十五、jenkins环境配置
1. jenkins包下载,下载地址:https://jenkins.io/download/ 版本:Jenkins 2.134,下载war包 2. JDK下载:下载地址:http://www.ora ...
- 阶段1 语言基础+高级_1-3-Java语言高级_09-基础加强_第2节 反射_11_反射_案例
student定义sleep方法 用反射+配置文件 定义配置文件 src下new file.新建 加载配置文件.Properties里面有一个load方法,可以加载.properties结尾的配置文件 ...
- 阶段1 语言基础+高级_1-3-Java语言高级_04-集合_02 泛型_5_定义和使用含有泛型的接口
定义泛型接口 Scanner的接口 接口的实现类.实现这个接口,规定数据类型为String类型 ArrayList是List接口的实现类 再看下List接口的源码 泛型实现类也定义为泛型 重写泛型的方 ...
- 编程语言分类,Python代码执行,应用程序使用文件的三步骤,变量,常量,垃圾回收机制
编程语言分为 机器语言(直接用二进制01跟计算机直接沟通交流,直接操作硬件) 优点:计算机能够直接读懂,速度快 缺点:开发效率极低 汇编语言(用简单的英文标签来表示二进制数,直接操作硬件) 优点:开发 ...
- 使用K近邻算法改进约会网站的配对效果
1 定义数据集导入函数 import numpy as np """ 函数说明:打开并解析文件,对数据进行分类:1 代表不喜欢,2 代表魅力一般,3 代表极具魅力 Par ...
- 前端 CSS层叠性 CSS选择器优先级
层叠性 层叠性:权重的标签覆盖掉了权重小的标签,说白了 ,就是被干掉了 权重:谁的权重大,浏览器就会显示谁的属性 我们现在已经学过了很多的选择器,也就是说在一个HTML页面中有很多种方式找到一个元素并 ...
- little-endian And big-endian
coming from http://zhidao.baidu.com/link?url=B_7AA_O6TkCVlKw9t_Xifu6TzpaFUiDEVkH1iTRT4vUGD0uRmazwduf ...
