【leetcode】1026. Maximum Difference Between Node and Ancestor
题目如下:
Given the
root
of a binary tree, find the maximum valueV
for which there exists different nodesA
andB
whereV = |A.val - B.val|
andA
is 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
2
and5000
.- Each node will have value between
0
and100000
.
解题思路:题目要求最大的差值的绝对值。对于任意一个节点来说,其可以得到的最大差值的绝对值只有在这四种情况中:与左子树的最大值,与左子树的最小值,与右子树的最大值,与右子树的最小值。所以只要遍历树,求出每个节点的最大差值,即可得到整棵树的最大差值。当然,为了提高效率,可以缓存每个节点的左子树的最大值、左子树的最小值、右子树的最大值、右子树的最小值。
代码如下:
# 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 参考资料 日期 题目 ...
随机推荐
- Ubuntu 系统搭建LNMP环境
当前Linux版本:Ubuntu16.04 一.安装Nginx 在终端中输入命令 " sudo apt-get install nginx ",在确认安装完成后,在浏览器中访问 l ...
- oracle体系结构基础
摘自:http://jianshi-dlw.iteye.com/blog/1554892 Oracle的体系结构大体上分为两部分:Instance(实例)和Database(数据库). Instanc ...
- 洛谷P4127同类分布
传送 我们要在dfs的板子里记录哪些量呢?当前填的所有数的和sum?当前填的数构成的数值all? sum可以留下,数值就扔掉叭.数值最大是1e18,要是留下,在g数组里有一维的大小是1e18.也许可以 ...
- 九、MySQL报错( (1292, u"Truncated incorrect DOUBLE value: '424a000000066'") result = self._query(query))
1.数据库sql语句:SELECT seat_id FROM netsale_order_seat os join netsale_order nor on os.order_code=nor.ord ...
- JSON基础,简单介绍
JSON(JavaScript Object Notation(记号.标记)) 是一种轻量级的数据交换格式.它基于JavaScript(Standard ECMA-262 3rd Edition - ...
- Vue实现音乐播放器(七):轮播图组件(二)
轮播图组件 <template> <div class="slider" ref="slider"> <div class=&qu ...
- Delphi XE2 之 FireMonkey 入门(16) - 滤镜: 实例测试
窗体上需要 TImage.TOpenDialog 和六个按钮. unit Unit1; interface uses System.SysUtils, System.Types, System.U ...
- Java Bean 使用包装类型 还是基本类型
参考:实体类中用基本类型好,还是用包装类型好_ - 牵牛花 - 博客园 int优缺点 优点: 1.用于Bean的时候,有默认值.比如自己拼接sql增加一个User时,会方便很多,不过现在都用ORM框架 ...
- jmeter逻辑控制详解(1)
逻辑控制器 Jmeter提供了多种逻辑控制器,下面进行讲解说明: 1.Simple Controller 简单控制器是最基本的控制器,对jmeter测试运行没有任何影响,可以将某些请求归集在一个简单控 ...
- SSM003/构建Maven单模块项目(一)
一.环境准备 1.开发工具:IDEA 2.JDK版本:jdk1.8 3.Maven版本:apache-maven-3.2.5 4.数据库mysql. 二.基于Maven构建web项目 Step1:Fi ...