训练营周赛3
This commit is contained in:
parent
7a74d91d79
commit
091b1e6666
21
src/main/java/com/code/leet/contest3/Solution1.java
Normal file
21
src/main/java/com/code/leet/contest3/Solution1.java
Normal file
@ -0,0 +1,21 @@
|
||||
package com.code.leet.contest3;
|
||||
|
||||
import com.code.leet.entiy.TreeNode;
|
||||
|
||||
public class Solution1 {
|
||||
public static void main(String[] args) {
|
||||
Solution1 solution = new Solution1();
|
||||
}
|
||||
|
||||
public int countNodes(TreeNode root, int val) {
|
||||
int count = 0;
|
||||
if (root != null) {
|
||||
if (root.val == val) {
|
||||
count++;
|
||||
}
|
||||
count += countNodes(root.left, val);
|
||||
count += countNodes(root.right, val);
|
||||
}
|
||||
return count;
|
||||
}
|
||||
}
|
25
src/main/java/com/code/leet/contest3/Solution2.java
Normal file
25
src/main/java/com/code/leet/contest3/Solution2.java
Normal file
@ -0,0 +1,25 @@
|
||||
package com.code.leet.contest3;
|
||||
|
||||
import com.code.leet.entiy.TreeNode;
|
||||
|
||||
public class Solution2 {
|
||||
public static void main(String[] args) {
|
||||
Solution2 solution = new Solution2();
|
||||
}
|
||||
|
||||
public int countInconsistentNode(TreeNode root1, TreeNode root2) {
|
||||
int count = 0;
|
||||
if (root1 == null && root2 != null) {
|
||||
count += 1 + countInconsistentNode(null, root2.left);
|
||||
count += countInconsistentNode(null, root2.right);
|
||||
} else if (root1 != null && root2 == null) {
|
||||
count += 1 + countInconsistentNode(null, root1.left);
|
||||
count += countInconsistentNode(null, root1.right);
|
||||
} else if (root1 != null && root2 != null) {
|
||||
count += root1.val == root2.val ? 0 : 2;
|
||||
count += countInconsistentNode(root1.left, root2.left);
|
||||
count += countInconsistentNode(root1.right, root2.right);
|
||||
}
|
||||
return count;
|
||||
}
|
||||
}
|
67
src/main/java/com/code/leet/contest3/Solution3.java
Normal file
67
src/main/java/com/code/leet/contest3/Solution3.java
Normal file
@ -0,0 +1,67 @@
|
||||
package com.code.leet.contest3;
|
||||
|
||||
import com.code.leet.entiy.TreeNode;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public class Solution3 {
|
||||
public static void main(String[] args) {
|
||||
Solution3 solution = new Solution3();
|
||||
solution.list1 = Arrays.asList(1, 1, 1, 2, 2);
|
||||
solution.solve(new int[][]{{0, 3}, {0, 2}, {1, 0}});
|
||||
}
|
||||
|
||||
private List<Integer> list1 = new ArrayList<>();
|
||||
|
||||
public int[] solve(int[][] operations) {
|
||||
int[] result = new int[list1.size()];
|
||||
for (int i = 0; i < list1.size(); i++) {
|
||||
result[i] = list1.get(i);
|
||||
}
|
||||
for (int i = 0; i < operations.length; i++) {
|
||||
if (operations[i][0] == 0) {
|
||||
for (int j = 0; j < list1.size(); j++) {
|
||||
if (operations[i][1] >= list1.get(j)) {
|
||||
list1.add(j, operations[i][1]);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
list1.remove(0);
|
||||
}
|
||||
result[i] = list1.get(0);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private List<Integer> list = new ArrayList<>();
|
||||
|
||||
public int[] solve(TreeNode root, int[][] operations) {
|
||||
dfs(root);
|
||||
int[] result = new int[operations.length];
|
||||
for (int i = 0; i < operations.length; i++) {
|
||||
if (operations[i][0] == 0) {
|
||||
for (int j = 0; j < list.size(); j++) {
|
||||
if (operations[i][1] >= list.get(j)) {
|
||||
list.add(j, operations[i][1]);
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
list.remove(0);
|
||||
}
|
||||
result[i] = list.get(0);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private void dfs(TreeNode root) {
|
||||
if (root == null) {
|
||||
return;
|
||||
}
|
||||
dfs(root.right);
|
||||
list.add(root.val);
|
||||
dfs(root.left);
|
||||
}
|
||||
}
|
35
src/main/java/com/code/leet/contest3/Solution4.java
Normal file
35
src/main/java/com/code/leet/contest3/Solution4.java
Normal file
@ -0,0 +1,35 @@
|
||||
package com.code.leet.contest3;
|
||||
|
||||
import com.code.leet.entiy.TreeNode;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class Solution4 {
|
||||
public static void main(String[] args) {
|
||||
Solution4 solution = new Solution4();
|
||||
}
|
||||
|
||||
List<Integer> list = new ArrayList<>();
|
||||
|
||||
public int splitTree(TreeNode root) {
|
||||
int total = dfs(root);
|
||||
list.remove(list.size() - 1);
|
||||
Collections.sort(list);
|
||||
int result = Integer.MAX_VALUE;
|
||||
for (Integer integer : list) {
|
||||
result = Math.min(result, Math.abs(total - 2 * integer));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private int dfs(TreeNode root) {
|
||||
if (root == null) {
|
||||
return 0;
|
||||
}
|
||||
int sum = root.val;
|
||||
sum += dfs(root.left);
|
||||
sum += dfs(root.right);
|
||||
list.add(sum);
|
||||
return sum;
|
||||
}
|
||||
}
|
68
src/main/java/com/code/leet/contest3/Solution5.java
Normal file
68
src/main/java/com/code/leet/contest3/Solution5.java
Normal file
@ -0,0 +1,68 @@
|
||||
package com.code.leet.contest3;
|
||||
|
||||
import com.code.leet.entiy.TreeNode;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class Solution5 {
|
||||
public static void main(String[] args) {
|
||||
Solution5 solution = new Solution5();
|
||||
TreeNode root = new TreeNode(4);
|
||||
|
||||
}
|
||||
|
||||
Map<TreeNode, List<TreeNode>> map = new HashMap<>();
|
||||
|
||||
public int getDistance(TreeNode root, int num1, int num2) {
|
||||
Queue<TreeNode> queue = new LinkedList<>();
|
||||
List<TreeNode> use = new ArrayList<>();
|
||||
for (TreeNode key : map.keySet()) {
|
||||
if (key.val == num1 || key.val == num2) {
|
||||
if (key.val == num2) {
|
||||
num2 = num1;
|
||||
}
|
||||
List<TreeNode> list = map.get(key);
|
||||
use.add(key);
|
||||
queue.addAll(list);
|
||||
break;
|
||||
}
|
||||
}
|
||||
int count = 1;
|
||||
while (!queue.isEmpty()) {
|
||||
int size = queue.size();
|
||||
for (int i = 0; i < size; i++) {
|
||||
TreeNode node = queue.poll();
|
||||
if(use.contains(node)){
|
||||
continue;
|
||||
}
|
||||
if(node.val==num2){
|
||||
return count;
|
||||
}
|
||||
queue.addAll(map.get(node));
|
||||
}
|
||||
count++;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
private void dfs(TreeNode root) {
|
||||
if (root == null) {
|
||||
return;
|
||||
}
|
||||
List<TreeNode> list = map.getOrDefault(root, new ArrayList<>());
|
||||
if (root.left != null) {
|
||||
list.add(root.left);
|
||||
List<TreeNode> llist = map.getOrDefault(root.left, new ArrayList<>());
|
||||
llist.add(root);
|
||||
map.put(root.left, llist);
|
||||
}
|
||||
if (root.right != null) {
|
||||
list.add(root.right);
|
||||
List<TreeNode> rlist = map.getOrDefault(root.right, new ArrayList<>());
|
||||
rlist.add(root);
|
||||
map.put(root.right, rlist);
|
||||
}
|
||||
map.put(root, list);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user