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 ...
随机推荐
- 修改Jupyter Notebook的默认打开路径
一: (也可以直接将删除的部分修改成所要存储的文件路径,之后三个步骤就可以省去了) 二: 打开Windows的cmd,在cmd中输入jupyter notebook --generate-config ...
- Django框架深入了解_05 (Django中的缓存、Django解决跨域流程(非简单请求,简单请求)、自动生成接口文档)
一.Django中的缓存: 前戏: 在动态网站中,用户所有的请求,服务器都会去数据库中进行相应的增,删,查,改,渲染模板,执行业务逻辑,最后生成用户看到的页面. 当一个网站的用户访问量很大的时候,每一 ...
- lsyncd实时同步工具
简介 Lysncd 实际上是lua语言封装了 inotify 和 rsync 工具,采用了 Linux 内核(2.6.13 及以后)里的 inotify 触发机制,然后通过rsync去差异同步,达到实 ...
- websocket-shap 函数Broadcast的使用方法
Broadcast:在websocket-shap函数的定义是:向WebSocket服务中的每个客户端发送数据,类似于广播的效果 如果要使用异步发送,可使用BroadcastAsync函数. 在源码中 ...
- flickity:支持触摸滑动,响应迅速的幻灯片轮播插件
简介:flickity 是一个支持触摸,响应迅速的幻灯片轮播插件.支持环绕滑动.自由滑动.组滑动.自动播放.延迟加载.视差滑动.图片滑动.兼容IE10+, Android 4+, Safari for ...
- 解除Ubuntu系统的root登录图形界面限制
Ubuntu18.04.1开发团队为了Ubuntu18.04.1系统的安全,默认root不能登录图形界面,普通用户需要使用root权限时,只能通过sudo [命令] [参数] 临时使用root权限,或 ...
- 【开发笔记】- 发送邮件(通过JavaMail发送)
前段时间在工作中用到了邮件发送监控的报警信息,今天在这个记录一下JavaMail的邮件工具类. 下边为用到的JavaMail的jar包的pom依赖.这里用的是JavaMail的1.4.6的版本. &l ...
- 小div在大div中垂直居中方式
代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8 ...
- insurance Alternative forms insuraunce保险
insurance Contents 1 English 1.1 Alternative forms 1.2 Etymology 1.3 Pronunciation 1.4 Noun 1.4.1 De ...
- MySQL Replication--复制延迟03--Seconds_Behind_Master计算
Seconds_Behind_Master计算原理 当从库上复制IO进程和复制SQL进程正常运行,且SQL线程处于执行状态而非等待IO进程同步BINLOG时,复制延迟时间计算如下: 复制延迟时间(Se ...