Skip to content

Commit f4659f1

Browse files
add 2265
1 parent e7f26c0 commit f4659f1

File tree

3 files changed

+63
-0
lines changed
  • paginated_contents/algorithms/3rd_thousand
  • src

3 files changed

+63
-0
lines changed

Diff for: paginated_contents/algorithms/3rd_thousand/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@
8888
| 2273 | [Find Resultant Array After Removing Anagrams](https://leetcode.com/problems/find-resultant-array-after-removing-anagrams/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2273.java) || Easy ||
8989
| 2270 | [Number of Ways to Split Array](https://leetcode.com/problems/number-of-ways-to-split-array/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2270.java) || Medium ||
9090
| 2269 | [Find the K-Beauty of a Number](https://leetcode.com/problems/find-the-k-beauty-of-a-number/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2269.java) || Easy ||
91+
| 2265 | [Count Nodes Equal to Average of Subtree](https://leetcode.com/problems/count-nodes-equal-to-average-of-subtree/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2265.java) || Medium |Tree, DFS
9192
| 2264 | [Largest 3-Same-Digit Number in String](https://leetcode.com/problems/largest-3-same-digit-number-in-string/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2264.java) || Easy ||
9293
| 2260 | [Minimum Consecutive Cards to Pick Up](https://leetcode.com/problems/minimum-consecutive-cards-to-pick-up/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2260.java) || Medium ||
9394
| 2259 | [Remove Digit From Number to Maximize Result](https://leetcode.com/problems/remove-digit-from-number-to-maximize-result/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2259.java) || Easy ||
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.fishercoder.solutions.thirdthousand;
2+
3+
import com.fishercoder.common.classes.TreeNode;
4+
5+
public class _2265 {
6+
public static class Solution1 {
7+
/**
8+
* When it comes to process all subtrees first, and then process the root, it's a good candidate to use post-order traversal recursion.
9+
* Credit: https://leetcode.com/problems/count-nodes-equal-to-average-of-subtree/editorial/
10+
*/
11+
12+
int count;
13+
14+
public int averageOfSubtree(TreeNode root) {
15+
postOrder(root);
16+
return count;
17+
}
18+
19+
private int[] postOrder(TreeNode root) {
20+
if (root == null) {
21+
return new int[2];
22+
}
23+
int[] left = postOrder(root.left);
24+
int[] right = postOrder(root.right);
25+
int nodeSum = left[0] + right[0] + root.val;
26+
int nodeCount = left[1] + right[1] + 1;
27+
if (root.val == nodeSum / nodeCount) {
28+
count++;
29+
}
30+
return new int[]{nodeSum, nodeCount};
31+
}
32+
}
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.fishercoder.thirdthousand;
2+
3+
import com.fishercoder.common.classes.TreeNode;
4+
import com.fishercoder.common.utils.TreeUtils;
5+
import com.fishercoder.solutions.thirdthousand._2265;
6+
import org.junit.jupiter.api.BeforeEach;
7+
import org.junit.jupiter.api.Test;
8+
9+
import java.util.Arrays;
10+
11+
import static org.junit.jupiter.api.Assertions.assertEquals;
12+
13+
public class _2265Test {
14+
private static _2265.Solution1 solution1;
15+
private static TreeNode root;
16+
17+
@BeforeEach
18+
public void setup() {
19+
solution1 = new _2265.Solution1();
20+
}
21+
22+
@Test
23+
public void test1() {
24+
root = TreeUtils.constructBinaryTree(Arrays.asList(4, 8, 5, 0, 1, null, 6));
25+
TreeUtils.printBinaryTree(root);
26+
assertEquals(5, solution1.averageOfSubtree(root));
27+
}
28+
29+
}

0 commit comments

Comments
 (0)