[LeetCode] 1026. Maximum Difference Between Node and Ancestor

摘要:
给定二叉树的根节点,找到不同节点A和B之间的最大值V,其中V=|A.val-BVal|,A是B的祖先。对于商业转载,请联系官方授权,对于非商业转载,则请注明来源。其思想是DFS预订单遍历。然后我们可以使用DFS预序遍历来记录当前路径上节点的最大值和最小值。当我们到达叶节点时,我们可以计算差值。

Given the root of a binary tree, find the maximum value V for which there exist 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:

[LeetCode] 1026. Maximum Difference Between Node and Ancestor第1张

Input: root = [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.

Example 2:

[LeetCode] 1026. Maximum Difference Between Node and Ancestor第2张

Input: root = [1,null,2,null,0,3]
Output: 3

Constraints:

  • The number of nodes in the tree is in the range [2, 5000].
  • 0 <= Node.val <= 105

节点与其祖先之间的最大差值。

给定二叉树的根节点 root,找出存在于不同节点 A 和 B 之间的最大值 V,其中 V = |A.val - B.val|,且 A 是 B 的祖先。

(如果 A 的任何子节点之一为 B,或者 A 的任何子节点是 B 的祖先,那么我们认为 A 是 B 的祖先)

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/maximum-difference-between-node-and-ancestor
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

思路是DFS前序遍历。题目让你找的是二叉树里面两个不同节点之间的最大差值,这两个节点要在同一条分支上。那么我们可以用DFS前序遍历,在遍历的过程中,记录一下当前路径上节点的最大值和最小值。当我们到达叶子节点的时候,则可以计算一下这个差值是多少。

时间O(n)

空间O(n)

Java实现

 1 /**
 2  * Definition for a binary tree node.
 3  * public class TreeNode {
 4  *     int val;
 5  *     TreeNode left;
 6  *     TreeNode right;
 7  *     TreeNode() {}
 8  *     TreeNode(int val) { this.val = val; }
 9  *     TreeNode(int val, TreeNode left, TreeNode right) {
10  *         this.val = val;
11  *         this.left = left;
12  *         this.right = right;
13  *     }
14  * }
15  */
16 class Solution {
17     public int maxAncestorDiff(TreeNode root) {
18         if (root == null) {
19             return 0;
20         }
21         return helper(root, root.val, root.val);
22     }
23 
24     private int helper(TreeNode root, int max, int min) {
25         // base case
26         if (root == null) {
27             return max - min;
28         }
29         max = Math.max(max, root.val);
30         min = Math.min(min, root.val);
31         int left = helper(root.left, max, min);
32         int right = helper(root.right, max, min);
33         return Math.max(left, right);
34     }
35 }

LeetCode 题目总结

免责声明:文章转载自《[LeetCode] 1026. Maximum Difference Between Node and Ancestor》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇iphone利用AudioQueue播放音频文件(mp3,aac,caf,wav等)【Vue】09 Webpack Part5 Vue组件化开发下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

印象笔记吐槽

前言 广告 电脑广告 手机广告 笔记编辑 超级笔记-无法搜索代码 超级笔记-编程语言支持 超级笔记-图片不同步,且无提示 MarkDown 各种收费项目 会员权益 资源包、字体包 其他 顶部自定义工具栏无效 双击顶部不能全屏 底部智能标签无法去掉 Mac 版无法启动(卸载重装仍无法启动) Android 客户端网络错误 后记 Gi...

leetcode编程框架——在IDE上模拟leetcode环境

1、为什么写这个为了用IDE!,这样不同的大题,复制粘贴就完事,真正帮你节约大把时间来思考题目。 import sys # 这里写解决问题的代码,和LeetCode就完全一样了 def solve(arr): pass if __name__ == '__main__': # 接收输入的逻辑,这里先把输入接收过来, 两种选择input()和sy...

视觉SLAM方向找工作经历

秋招已经过去了两个多月了,一直拖到现在2017年快完了才"舍得"写== 先说一下自己的基本情况,985硕士一枚,有还不错的一作会议论文,一直专注在视觉SLAM和三维重建方面,SLAM和三维重建内容都很多,我懂的并不够精。 虽然心里一直想早点开始准备,但是实验室的事情也比较多,还是拖到了2017年年初才开始花时间去准备,同时还在写一篇论文。实习很重要,实习基...

工具推荐--刷LeetCode的神器

本文首发于微信公众号:【坂本先生】,文章地址为:https://mp.weixin.qq.com/s/vHv5hO8nils_g2VSKwu1Cg如有转载请标明出处 今天给大家安利一款快速刷LeetCode的工具,能够让你专注于题目本身,而不是如何去建立题目。这个工具是基于IDEA的,名叫LeetCode Editor,它的官方GitHub地址为:http...

LeetCode 26

一、问题描述 Description: Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. For example: Given input array nums =...

leetcode 36 有效的数独 哈希表 unordered_set unordersd_map 保存状态 leetcode 37 解数独

leetcode 36 感觉就是遍历。 保存好状态,就是各行各列还有各分区divide的情况 用数组做。 空间小时间大 class Solution { public: bool isValidSudoku(vector<vector<char>>& board) { int row[9][9]={...