原题链接在这里: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. Word 插入目录详细教程 -- 视频教程(6)

    >> 视频教程链接:B站,速度快,清晰 更多关于插入目录的方法,参看:Word插入目录系列 未完 ...... 点击访问原文(进入后根据右侧标签,快速定位到本文)

  2. 【C++/C】指针基本用法简介-A Summary of basic usage of Pointers.

    基于谭浩强老师<C++程序设计(第三版)>做简要Summary.(2019-07-24) 一.数组与指针 1. 指针数组 一个数组,其元素均为指针类型数据,该数组称为指针数组.(type_ ...

  3. C 猜猜猜😀文字小游戏

    前言 - 随机性 随机数生成 - https://zh.wikipedia.org/wiki/%E9%9A%8F%E6%9C%BA%E6%95%B0%E7%94%9F%E6%88%90 没啥事情, 写 ...

  4. golang在linux下的安装操作

  5. Mysql练习题(1)

    表名和字段------------------------------------------------------------------------------–1.学生表Student(s_i ...

  6. CapsLock Enhancement via AutoHotKey

    上次写了一篇博文,讲如何通过AutoHotKey改造CaspLock,使其成为一个方便的编辑按键,并特意给出了设计的思路方便参考. 见地址:http://www.cnblogs.com/Vonng/p ...

  7. CSP J/S 2019受虐记

    一枚蒟蒻的游记~ 提高组DAY1 不是说每场考试都有一道签到题吗 那我tm读了三遍题硬是没找到一道水题是怎么回事(是我太弱了吗) 没办法,硬着头皮做T1 暴力写法...期望得分30pts 于是...在 ...

  8. 英伟达 cuda 开发套件下载

    下载地址 https://developer.nvidia.com/cuda-toolkit 安装比较简单,就不多说了.

  9. IDEA自动清理优化import包

    IDEA自动清理优化import包 直接上图: Add unambiguous imports on the fly:快速添加明确的导入. Optimize imports on the fly:快速 ...

  10. android ViewFlipper(翻转视图) 使用

    1.布局文件 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns ...