Skip to content

Commit cf6edfd

Browse files
add 1026
1 parent 8a3c77b commit cf6edfd

File tree

3 files changed

+75
-0
lines changed

3 files changed

+75
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,7 @@ _If you like this project, please leave me a star._ ★
482482
|1033|[Moving Stones Until Consecutive](https://leetcode.com/problems/moving-stones-until-consecutive/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1033.java) | |Easy|Math|
483483
|1030|[Matrix Cells in Distance Order](https://leetcode.com/problems/matrix-cells-in-distance-order/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1030.java) | |Easy|
484484
|1029|[Two City Scheduling](https://leetcode.com/problems/two-city-scheduling/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1029.java) | |Easy|
485+
|1026|[Maximum Difference Between Node and Ancestor](https://leetcode.com/problems/maximum-difference-between-node-and-ancestor/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1026.java) | |Medium|Tree, DFS, Binary Tree|
485486
|1025|[Divisor Game](https://leetcode.com/problems/divisor-game/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1025.java) | |Easy|Math, DP, Brainteaser, Game Theory|
486487
|1024|[Video Stitching](https://leetcode.com/problems/video-stitching/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1024.java) | |Medium|Array, DP, Greedy|
487488
|1022|[Sum of Root To Leaf Binary Numbers](https://leetcode.com/problems/sum-of-root-to-leaf-binary-numbers/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1022.java) | |Easy|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.fishercoder.solutions;
2+
3+
import com.fishercoder.common.classes.TreeNode;
4+
5+
public class _1026 {
6+
public static class Solution1 {
7+
/**
8+
* My completely original solution on 12/31/2021.
9+
*/
10+
int maxDiff = 0;
11+
12+
public int maxAncestorDiff(TreeNode root) {
13+
dfs(root);
14+
return maxDiff;
15+
}
16+
17+
private void dfs(TreeNode root) {
18+
if (root == null) {
19+
return;
20+
}
21+
int[] minmax = new int[]{root.val, root.val};
22+
findMinMax(root, minmax);
23+
maxDiff = Math.max(maxDiff, Math.max(Math.abs(root.val - minmax[0]), Math.abs(minmax[1] - root.val)));
24+
dfs(root.left);
25+
dfs(root.right);
26+
}
27+
28+
private void findMinMax(TreeNode root, int[] minmax) {
29+
if (root == null) {
30+
return;
31+
}
32+
if (root.left != null) {
33+
minmax[0] = Math.min(root.left.val, minmax[0]);
34+
minmax[1] = Math.max(root.left.val, minmax[1]);
35+
}
36+
if (root.right != null) {
37+
minmax[0] = Math.min(root.right.val, minmax[0]);
38+
minmax[1] = Math.max(root.right.val, minmax[1]);
39+
}
40+
findMinMax(root.left, minmax);
41+
findMinMax(root.right, minmax);
42+
}
43+
}
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.common.classes.TreeNode;
4+
import com.fishercoder.common.utils.TreeUtils;
5+
import com.fishercoder.solutions._1026;
6+
import org.junit.Test;
7+
8+
import java.util.Arrays;
9+
10+
import static org.junit.Assert.assertEquals;
11+
12+
public class _1026Test {
13+
private static _1026.Solution1 solution1;
14+
private static TreeNode root;
15+
16+
@Test
17+
public void test1() {
18+
solution1 = new _1026.Solution1();
19+
root = TreeUtils.constructBinaryTree(Arrays.asList(8, 3, 10, 1, 6, null, 14, null, null, 4, 7, 13));
20+
assertEquals(7, solution1.maxAncestorDiff(root));
21+
}
22+
23+
@Test
24+
public void test2() {
25+
solution1 = new _1026.Solution1();
26+
root = TreeUtils.constructBinaryTree(Arrays.asList(1, null, 2, null, 0, 3));
27+
assertEquals(3, solution1.maxAncestorDiff(root));
28+
}
29+
30+
}

0 commit comments

Comments
 (0)