LeetCode 1026. Maximum Difference Between Node and Ancestor
原题链接在这里:https://leetcode.com/problems/maximum-difference-between-node-and-ancestor/
题目:
Given the root of a binary tree, find the maximum value V for which there exists different nodes A and B where V = |A.val - B.val| and A is an ancestor of B.
(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.
题解:
Perform DFS top down. On each level, Calculate maxmimum difference and update minimum and maximum value.
Time Complexity: O(n).
Space: O(h).
AC Java:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
int res = 0; public int maxAncestorDiff(TreeNode root) {
if(root == null){
return res;
} dfs(root, root.val, root.val);
return res;
} private void dfs(TreeNode root, int min, int max){
if(root == null){
return;
} res = Math.max(res, Math.max(Math.abs(root.val - min), Math.abs(max-root.val)));
min = Math.min(min, root.val);
max = Math.max(max, root.val);
dfs(root.left, min, max);
dfs(root.right, min, max);
}
}
LeetCode 1026. Maximum Difference Between Node and Ancestor的更多相关文章
- 【leetcode】1026. Maximum Difference Between Node and Ancestor
题目如下: Given the root of a binary tree, find the maximum value V for which there exists different nod ...
- [Swift]LeetCode1026. 节点与其祖先之间的最大差值 | Maximum Difference Between Node and Ancestor
Given the root of a binary tree, find the maximum value V for which there exists different nodes A a ...
- [LeetCode] 164. Maximum Gap 求最大间距
Given an unsorted array, find the maximum difference between the successive elements in its sorted f ...
- LeetCode 164. Maximum Gap[翻译]
164. Maximum Gap 164. 最大间隔 Given an unsorted array, find the maximum difference between the successi ...
- 【leetcode】Maximum Gap
Maximum Gap Given an unsorted array, find the maximum difference between the successive elements in ...
- 【leetcode】Maximum Gap(hard)★
Given an unsorted array, find the maximum difference between the successive elements in its sorted f ...
- ✡ leetcode 164. Maximum Gap 寻找最大相邻数字差 --------- java
Given an unsorted array, find the maximum difference between the successive elements in its sorted f ...
- LeetCode题解-----Maximum Gap
题目描述: Given an unsorted array, find the maximum difference between the successive elements in its so ...
- Java for LeetCode 164 Maximum Gap
Given an unsorted array, find the maximum difference between the successive elements in its sorted f ...
随机推荐
- Django框架深入了解_05 (Django中的缓存、Django解决跨域流程(非简单请求,简单请求)、自动生成接口文档)
一.Django中的缓存: 前戏: 在动态网站中,用户所有的请求,服务器都会去数据库中进行相应的增,删,查,改,渲染模板,执行业务逻辑,最后生成用户看到的页面. 当一个网站的用户访问量很大的时候,每一 ...
- 前端框架之Bootstrap框架
下载地址:https://v3.bootcss.com/,下载Bootstrap3版本 下载之后把文件中不需要的文件都删掉 需要获取的样式代码,可以直接从这些地方找到,然后复制 一.HTML页面导入文 ...
- STM32串口复用关系&printf重定义
串口复用 什么是普通功能输入输出?普通功能输入输出类似于:大厅<—>门<—>室外的关系,大厅只需要经过大门即可到室外,从室外经过门也可以到达大厅. 什么是复用工功能输入输出?复 ...
- 【数据结构】6.java源码ArrayList
关于ArrayList的源码关注点 1.从底层数据结构,扩容策略2.ArrayList的增删改查3.特殊处理重点关注4.遍历的速度,随机访问和iterator访问效率对比 1.从底层数据结构,扩容策略 ...
- golang 之 sql
golang提供了sql包查询数据 建立连接 导入第三方包 import( "database/sql" _"github.com/go-sql-driver/mysql ...
- wildfly添加JNDI驱动配置
wildfly-9.0.1.Final/modules目录下新建com文件夹cd com && mkdir mysql && cd mysql && m ...
- pytest_参数化parametrize
前言 pytest.mark.parametrize装饰器可以实现测试用例参数化. parametrizing 1.这里是一个实现检查一定的输入和期望输出测试功能的典型例子 import pytest ...
- jwt的思考
什么是jwt jwt的问题 jwt的是实践 https://www.pingidentity.com/en/company/blog/posts/2019/jwt-security-nobody-ta ...
- 创建maven父项目以及子项目
创建maven父项目以及子项目(Eclipse创建Maven Project跟Maven Module)https://blog.csdn.net/Mrsanger/article/details/8 ...
- (转)数据库_不懂数据库索引的底层原理?那是因为你心里没点BTree
原文地址:https://www.cnblogs.com/sujing/p/11110292.html 要了解数据库索引的底层原理,我们就得先了解一种叫树的数据结构,而树中很经典的一种数据结构就是二叉 ...