原题链接在这里: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的更多相关文章

  1. 【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 ...

  2. [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 ...

  3. [LeetCode] 164. Maximum Gap 求最大间距

    Given an unsorted array, find the maximum difference between the successive elements in its sorted f ...

  4. LeetCode 164. Maximum Gap[翻译]

    164. Maximum Gap 164. 最大间隔 Given an unsorted array, find the maximum difference between the successi ...

  5. 【leetcode】Maximum Gap

    Maximum Gap Given an unsorted array, find the maximum difference between the successive elements in ...

  6. 【leetcode】Maximum Gap(hard)★

    Given an unsorted array, find the maximum difference between the successive elements in its sorted f ...

  7. ✡ leetcode 164. Maximum Gap 寻找最大相邻数字差 --------- java

    Given an unsorted array, find the maximum difference between the successive elements in its sorted f ...

  8. LeetCode题解-----Maximum Gap

    题目描述: Given an unsorted array, find the maximum difference between the successive elements in its so ...

  9. Java for LeetCode 164 Maximum Gap

    Given an unsorted array, find the maximum difference between the successive elements in its sorted f ...

随机推荐

  1. 【C语言】 strlen()入参空指针导致段错误

    背景: 在工作中调试sqlite3相关代码的时候,调用printf()打印sqlite3_exec()的执行日志:因为sqlite3_exec()保存日志的参数传入时为NULL,且没有执行错误,所以再 ...

  2. c++项目经验分享

    1.C++的const比C语言#define更好的原因? 首先,它能够明确指定类型,有类型检查功能. 其次,可以使用C++的作用域规则将定义限制在特定的函数[常函数]或文件中. 第三,可以将const ...

  3. Python爬虫之旅(一):小白也能懂的爬虫入门

    Python爬虫之旅(一):小白也能懂的爬虫入门   爬虫是什么 爬虫就是按照一定的规则,去抓取网页中的信息.爬虫流程大致分为以下几步: 向目标网页发送请求 获取请求的响应内容 按照一定的规则解析返回 ...

  4. tkinter基础-输入框、文本框

    本节内容 了解输入框.文本框的使用方法 利用1制作简易界面 首先明确上面由几个元素组成:该界面由界面标题,输入框.两个按钮.文本框组成. 该界面我们需要实现的功能: 在输入框中输入文字,点击inser ...

  5. 论文笔记: Deep Learning based Recommender System: A Survey and New Perspectives

    (聊两句,突然记起来以前一个学长说的看论文要能够把论文的亮点挖掘出来,合理的进行概括23333) 传统的推荐系统方法获取的user-item关系并不能获取其中非线性以及非平凡的信息,获取非线性以及非平 ...

  6. 记一次线上问题排查:C#可选参数的坑

    线上报了大量异常,错误信息为:找不到XX方法实现 代码调用关系是: 查看代码历史记录,发现最近上线前对 GetUserDottedLineSuperiors 方法做过修改,增加了一个可选参数. 跟相关 ...

  7. 2019 多益网络java面试笔试题 (含面试题解析)

      本人5年开发经验.18年年底开始跑路找工作,在互联网寒冬下成功拿到阿里巴巴.今日头条.多益网络等公司offer,岗位是Java后端开发,因为发展原因最终选择去了多益网络,入职一年时间了,也成为了面 ...

  8. Win 10 无法锁屏,快捷键win+L失效

    快捷键win+L 一直在使用,忽然之间不知道按错了什么 Win 10 无法锁屏,快捷键win+L失效,按win+L后出来的是输入法 应该是键盘的Windows键锁住了,按Fn+windows键解锁

  9. 交换ESCHAUNGE英语ESCHAUNGE交易所

    exchange From Middle English eschaunge, borrowed from Anglo-Norman eschaunge exchange 1.An act of ex ...

  10. Audio Queue Services Programming Guide(音频队列服务编程指南)

    Audio Queue Services 的苹果官方文档: https://developer.apple.com/library/ios/documentation/MusicAudio/Conce ...