Skip to content

Commit ee0739b

Browse files
authored
feat: add solutions to lc problem: No.1026 (doocs#2538)
No.1026.Maximum Difference Between Node and Ancestor
1 parent 5b47413 commit ee0739b

File tree

4 files changed

+124
-5
lines changed

4 files changed

+124
-5
lines changed

solution/1000-1099/1026.Maximum Difference Between Node and Ancestor/README.md

+38-2
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050

5151
### 方法一:DFS
5252

53-
对于每个节点,求其与祖先节点的最大差值,我们只需要求出该节点与祖先节点最大值和最小值的差值,取所有差值的最大值即可
53+
对于每个节点,求其与祖先节点的最大差值,我们只需要求出该节点与祖先节点最大值和最小值的差值。取所有节点与祖先节点差值的最大值即可
5454

5555
因此,我们设计一个函数 $dfs(root, mi, mx)$,表示当前搜索到的节点为 $root$,其祖先节点的最大值为 $mx$,最小值为 $mi$,函数内更新最大差值 $ans$。
5656

@@ -75,7 +75,7 @@
7575
# self.right = right
7676
class Solution:
7777
def maxAncestorDiff(self, root: Optional[TreeNode]) -> int:
78-
def dfs(root, mi, mx):
78+
def dfs(root: Optional[TreeNode], mi: int, mx: int):
7979
if root is None:
8080
return
8181
nonlocal ans
@@ -255,6 +255,42 @@ var maxAncestorDiff = function (root) {
255255
};
256256
```
257257

258+
```cs
259+
/**
260+
* Definition for a binary tree node.
261+
* public class TreeNode {
262+
* public int val;
263+
* public TreeNode left;
264+
* public TreeNode right;
265+
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
266+
* this.val = val;
267+
* this.left = left;
268+
* this.right = right;
269+
* }
270+
* }
271+
*/
272+
public class Solution {
273+
private int ans;
274+
275+
public int MaxAncestorDiff(TreeNode root) {
276+
dfs(root, root.val, root.val);
277+
return ans;
278+
}
279+
280+
private void dfs(TreeNode root, int mi, int mx) {
281+
if (root == null) {
282+
return;
283+
}
284+
int x = Math.Max(Math.Abs(mi - root.val), Math.Abs(mx - root.val));
285+
ans = Math.Max(ans, x);
286+
mi = Math.Min(mi, root.val);
287+
mx = Math.Max(mx, root.val);
288+
dfs(root.left, mi, mx);
289+
dfs(root.right, mi, mx);
290+
}
291+
}
292+
```
293+
258294
<!-- tabs:end -->
259295

260296
<!-- end -->

solution/1000-1099/1026.Maximum Difference Between Node and Ancestor/README_EN.md

+52-2
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,21 @@ Among all possible differences, the maximum value of 7 is obtained by |8 - 1| =
4040

4141
## Solutions
4242

43-
### Solution 1
43+
### Solution 1: DFS
44+
45+
For each node, to find the maximum difference with its ancestor nodes, we only need to find the difference between the maximum and minimum values of the ancestor nodes. The maximum difference among all nodes and their ancestor nodes is the answer.
46+
47+
Therefore, we design a function $dfs(root, mi, mx)$, where the current node being searched is $root$, the maximum value of its ancestor nodes is $mx$, and the minimum value is $mi$. The function updates the maximum difference $ans$.
48+
49+
The logic of the function $dfs(root, mi, mx)$ is as follows:
50+
51+
- If $root$ is null, return directly.
52+
- Otherwise, we update $ans = max(ans, |mi - root.val|, |mx - root.val|)$.
53+
- Then update $mi = min(mi, root.val)$, $mx = max(mx, root.val)$, and recursively search the left and right subtrees.
54+
55+
In the main function, we call $dfs(root, root.val, root.val)$, and finally return $ans$.
56+
57+
The time complexity is $O(n)$, and the space complexity is $O(n)$. Where $n$ is the number of nodes in the binary tree.
4458

4559
<!-- tabs:start -->
4660

@@ -53,7 +67,7 @@ Among all possible differences, the maximum value of 7 is obtained by |8 - 1| =
5367
# self.right = right
5468
class Solution:
5569
def maxAncestorDiff(self, root: Optional[TreeNode]) -> int:
56-
def dfs(root, mi, mx):
70+
def dfs(root: Optional[TreeNode], mi: int, mx: int):
5771
if root is None:
5872
return
5973
nonlocal ans
@@ -233,6 +247,42 @@ var maxAncestorDiff = function (root) {
233247
};
234248
```
235249

250+
```cs
251+
/**
252+
* Definition for a binary tree node.
253+
* public class TreeNode {
254+
* public int val;
255+
* public TreeNode left;
256+
* public TreeNode right;
257+
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
258+
* this.val = val;
259+
* this.left = left;
260+
* this.right = right;
261+
* }
262+
* }
263+
*/
264+
public class Solution {
265+
private int ans;
266+
267+
public int MaxAncestorDiff(TreeNode root) {
268+
dfs(root, root.val, root.val);
269+
return ans;
270+
}
271+
272+
private void dfs(TreeNode root, int mi, int mx) {
273+
if (root == null) {
274+
return;
275+
}
276+
int x = Math.Max(Math.Abs(mi - root.val), Math.Abs(mx - root.val));
277+
ans = Math.Max(ans, x);
278+
mi = Math.Min(mi, root.val);
279+
mx = Math.Max(mx, root.val);
280+
dfs(root.left, mi, mx);
281+
dfs(root.right, mi, mx);
282+
}
283+
}
284+
```
285+
236286
<!-- tabs:end -->
237287

238288
<!-- end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* public int val;
5+
* public TreeNode left;
6+
* public TreeNode right;
7+
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
8+
* this.val = val;
9+
* this.left = left;
10+
* this.right = right;
11+
* }
12+
* }
13+
*/
14+
public class Solution {
15+
private int ans;
16+
17+
public int MaxAncestorDiff(TreeNode root) {
18+
dfs(root, root.val, root.val);
19+
return ans;
20+
}
21+
22+
private void dfs(TreeNode root, int mi, int mx) {
23+
if (root == null) {
24+
return;
25+
}
26+
int x = Math.Max(Math.Abs(mi - root.val), Math.Abs(mx - root.val));
27+
ans = Math.Max(ans, x);
28+
mi = Math.Min(mi, root.val);
29+
mx = Math.Max(mx, root.val);
30+
dfs(root.left, mi, mx);
31+
dfs(root.right, mi, mx);
32+
}
33+
}

solution/1000-1099/1026.Maximum Difference Between Node and Ancestor/Solution.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
# self.right = right
77
class Solution:
88
def maxAncestorDiff(self, root: Optional[TreeNode]) -> int:
9-
def dfs(root, mi, mx):
9+
def dfs(root: Optional[TreeNode], mi: int, mx: int):
1010
if root is None:
1111
return
1212
nonlocal ans

0 commit comments

Comments
 (0)