From 091b1e6666c87b88bf25439007624f2e017dad27 Mon Sep 17 00:00:00 2001 From: "huangge1199@hotmail.com" Date: Sun, 25 Jul 2021 18:18:21 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AE=AD=E7=BB=83=E8=90=A5=E5=91=A8=E8=B5=9B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/code/leet/contest3/Solution1.java | 21 ++++++ .../com/code/leet/contest3/Solution2.java | 25 +++++++ .../com/code/leet/contest3/Solution3.java | 67 ++++++++++++++++++ .../com/code/leet/contest3/Solution4.java | 35 ++++++++++ .../com/code/leet/contest3/Solution5.java | 68 +++++++++++++++++++ 5 files changed, 216 insertions(+) create mode 100644 src/main/java/com/code/leet/contest3/Solution1.java create mode 100644 src/main/java/com/code/leet/contest3/Solution2.java create mode 100644 src/main/java/com/code/leet/contest3/Solution3.java create mode 100644 src/main/java/com/code/leet/contest3/Solution4.java create mode 100644 src/main/java/com/code/leet/contest3/Solution5.java diff --git a/src/main/java/com/code/leet/contest3/Solution1.java b/src/main/java/com/code/leet/contest3/Solution1.java new file mode 100644 index 0000000..ac35f61 --- /dev/null +++ b/src/main/java/com/code/leet/contest3/Solution1.java @@ -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; + } +} diff --git a/src/main/java/com/code/leet/contest3/Solution2.java b/src/main/java/com/code/leet/contest3/Solution2.java new file mode 100644 index 0000000..c08b20c --- /dev/null +++ b/src/main/java/com/code/leet/contest3/Solution2.java @@ -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; + } +} diff --git a/src/main/java/com/code/leet/contest3/Solution3.java b/src/main/java/com/code/leet/contest3/Solution3.java new file mode 100644 index 0000000..6da52a8 --- /dev/null +++ b/src/main/java/com/code/leet/contest3/Solution3.java @@ -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 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 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); + } +} diff --git a/src/main/java/com/code/leet/contest3/Solution4.java b/src/main/java/com/code/leet/contest3/Solution4.java new file mode 100644 index 0000000..133807a --- /dev/null +++ b/src/main/java/com/code/leet/contest3/Solution4.java @@ -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 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; + } +} diff --git a/src/main/java/com/code/leet/contest3/Solution5.java b/src/main/java/com/code/leet/contest3/Solution5.java new file mode 100644 index 0000000..b7ad007 --- /dev/null +++ b/src/main/java/com/code/leet/contest3/Solution5.java @@ -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> map = new HashMap<>(); + + public int getDistance(TreeNode root, int num1, int num2) { + Queue queue = new LinkedList<>(); + List use = new ArrayList<>(); + for (TreeNode key : map.keySet()) { + if (key.val == num1 || key.val == num2) { + if (key.val == num2) { + num2 = num1; + } + List 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 list = map.getOrDefault(root, new ArrayList<>()); + if (root.left != null) { + list.add(root.left); + List llist = map.getOrDefault(root.left, new ArrayList<>()); + llist.add(root); + map.put(root.left, llist); + } + if (root.right != null) { + list.add(root.right); + List rlist = map.getOrDefault(root.right, new ArrayList<>()); + rlist.add(root); + map.put(root.right, rlist); + } + map.put(root, list); + } + +}