原题链接在这里: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. 列表,元组以及range

    列表,元组以及range 一.列表(list) 列表是数据类型之一,它有序,可变,支持索引 作用:存储数据,支持的数据类型很多:字符串,数字,布尔值,列表等 # 定义一个列表 lst = ['alex ...

  2. 【LEETCODE】69、动态规划,easy,medium级别,题目:198、139、221

    package y2019.Algorithm.dynamicprogramming.easy; /** * @ProjectName: cutter-point * @Package: y2019. ...

  3. [cf 1194 D] 1-2-K Game

    (当时让这道sb题卡住了,我比sb还sb) 题意: n个东西,两个人轮流取,每次可以取走1个,2个或k个,不能取的人输,求谁必胜. $0\leq n \leq 10^{9},3\leq k \leq ...

  4. MySql5.7 json查询

    create table t1(name json); insert into t1 values(’ { “hello”: “song”, “num”: 111, “obj”: { “who”: “ ...

  5. 初学zipkin搭建链路追踪服务注意事项

    版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/fsy9595887/article/det ...

  6. SQL Server——死锁查看

    一.通过语句查看 --查询哪些死锁SELECT request_session_id spid, OBJECT_NAME( resource_associated_entity_id ) tableN ...

  7. java之spring mvc之拦截器

    1. springmvc 中的拦截器是由实现 HandlerInterceptor 或者继承 HandlerInterceptorAdapter 来实现的. 2. 自定义实现一个拦截器的步骤: a). ...

  8. 2019 网易java面试笔试题 (含面试题解析)

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

  9. P2711 小行星 (最大流)

    题目 P2711 小行星 解析 这道题挺巧妙的,乍一看是空间上的,无从下手,稍微转换一下就可以了. 看到题目,求消除这些行星的最少次数,就是求最小割,也就是求最大流,考虑怎样建图. 考虑当我们消去一个 ...

  10. Windows查看端口使用状况(转)

    转:https://www.cnblogs.com/lixuwu/p/5898354.html 阅读目录 1 查看windows所有端口进程 2 查询指定端口 使用端口是我们在进行远程或者打印机等都会 ...