Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitcf6edfd

Browse files
add 1026
1 parent8a3c77b commitcf6edfd

File tree

3 files changed

+75
-0
lines changed

3 files changed

+75
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
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|
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
packagecom.fishercoder.solutions;
2+
3+
importcom.fishercoder.common.classes.TreeNode;
4+
5+
publicclass_1026 {
6+
publicstaticclassSolution1 {
7+
/**
8+
* My completely original solution on 12/31/2021.
9+
*/
10+
intmaxDiff =0;
11+
12+
publicintmaxAncestorDiff(TreeNoderoot) {
13+
dfs(root);
14+
returnmaxDiff;
15+
}
16+
17+
privatevoiddfs(TreeNoderoot) {
18+
if (root ==null) {
19+
return;
20+
}
21+
int[]minmax =newint[]{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+
privatevoidfindMinMax(TreeNoderoot,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+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
packagecom.fishercoder;
2+
3+
importcom.fishercoder.common.classes.TreeNode;
4+
importcom.fishercoder.common.utils.TreeUtils;
5+
importcom.fishercoder.solutions._1026;
6+
importorg.junit.Test;
7+
8+
importjava.util.Arrays;
9+
10+
importstaticorg.junit.Assert.assertEquals;
11+
12+
publicclass_1026Test {
13+
privatestatic_1026.Solution1solution1;
14+
privatestaticTreeNoderoot;
15+
16+
@Test
17+
publicvoidtest1() {
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+
publicvoidtest2() {
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)

[8]ページ先頭

©2009-2025 Movatter.jp