diff --git a/GeeksForGeeks/May/16-5-24/GFG.java b/GeeksForGeeks/May/16-5-24/GFG.java new file mode 100644 index 0000000..39b0392 --- /dev/null +++ b/GeeksForGeeks/May/16-5-24/GFG.java @@ -0,0 +1,104 @@ +//{ Driver Code Starts +import java.io.*; +import java.util.*; + +class IntMatrix { + public static int[][] input(BufferedReader br, int n, int m) throws IOException { + int[][] mat = new int[n][]; + + for (int i = 0; i < n; i++) { + String[] s = br.readLine().trim().split(" "); + mat[i] = new int[s.length]; + for (int j = 0; j < s.length; j++) mat[i][j] = Integer.parseInt(s[j]); + } + + return mat; + } + + public static void print(int[][] m) { + for (var a : m) { + for (int e : a) System.out.print(e + " "); + System.out.println(); + } + } + + public static void print(ArrayList> m) { + for (var a : m) { + for (int e : a) System.out.print(e + " "); + System.out.println(); + } + } +} + +class GFG { + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + int t; + t = Integer.parseInt(br.readLine()); + while (t-- > 0) { + + int n; + n = Integer.parseInt(br.readLine()); + + int[][] edges = IntMatrix.input(br, n - 1, 2); + + Solution obj = new Solution(); + int res = obj.minimumEdgeRemove(n, edges); + + System.out.println(res); + } + } +} + +// } Driver Code Ends + + + +class Solution +{ + public int minimumEdgeRemove(int n, int[][] edges) + { + // code here + Map> map = new HashMap<>(); + for(int i=1; i<=n+1; i++) + map.put(i, new ArrayList<>()); + + for(int [] edge : edges) + { + List list = map.getOrDefault(edge[0], new ArrayList<>()); + list.add(edge[1]); + map.put(edge[0], list); + list = map.getOrDefault(edge[1], new ArrayList<>()); + list.add(edge[0]); + map.put(edge[1], list); + } + + boolean [] vis = new boolean[n+1]; + int [] ans = new int[1]; + + dfs(1, map, vis, ans); + + return ans[0]; + } + + + private int dfs(int root, Map> map, boolean[] vis, int[] ans) + { + vis[root] = true; + int len = 1; + for(int child : map.get(root)) + { + if(vis[child] == false) + { + int res = dfs(child, map, vis, ans); + + if(res % 2 == 0) + ans[0]++; + else + len += res; + } + } + + return len; + } +} diff --git a/GeeksForGeeks/May/16-5-24/README.md b/GeeksForGeeks/May/16-5-24/README.md new file mode 100644 index 0000000..7384c4b --- /dev/null +++ b/GeeksForGeeks/May/16-5-24/README.md @@ -0,0 +1,2 @@ +Time complexity - O(n) +Space complexity - O(n) diff --git a/LeetCode/May/16-5-24/README.md b/LeetCode/May/16-5-24/README.md new file mode 100644 index 0000000..722dfdc --- /dev/null +++ b/LeetCode/May/16-5-24/README.md @@ -0,0 +1,2 @@ +Time complexity - O(n) +Space complexity - O(h) diff --git a/LeetCode/May/16-5-24/Solution.java b/LeetCode/May/16-5-24/Solution.java new file mode 100644 index 0000000..624c37e --- /dev/null +++ b/LeetCode/May/16-5-24/Solution.java @@ -0,0 +1,28 @@ +/** + * Definition for a binary tree node. + * public class TreeNode { + * int val; + * TreeNode left; + * TreeNode right; + * TreeNode() {} + * TreeNode(int val) { this.val = val; } + * TreeNode(int val, TreeNode left, TreeNode right) { + * this.val = val; + * this.left = left; + * this.right = right; + * } + * } + */ +class Solution +{ + public boolean evaluateTree(TreeNode root) + { + if (root.val < 2) + return root.val == 1; + // OR + if (root.val == 2) + return evaluateTree(root.left) || evaluateTree(root.right); + // AND + return evaluateTree(root.left) && evaluateTree(root.right); + } +}