Skip to content

Commit

Permalink
Added codes for 16 May
Browse files Browse the repository at this point in the history
  • Loading branch information
Tanmay-312 committed May 16, 2024
1 parent ace56e4 commit 3041691
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 0 deletions.
104 changes: 104 additions & 0 deletions GeeksForGeeks/May/16-5-24/GFG.java
Original file line number Diff line number Diff line change
@@ -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<ArrayList<Integer>> 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<Integer, List<Integer>> map = new HashMap<>();
for(int i=1; i<=n+1; i++)
map.put(i, new ArrayList<>());

for(int [] edge : edges)
{
List<Integer> 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<Integer, List<Integer>> 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;
}
}
2 changes: 2 additions & 0 deletions GeeksForGeeks/May/16-5-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(n)
Space complexity - O(n)
2 changes: 2 additions & 0 deletions LeetCode/May/16-5-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(n)
Space complexity - O(h)
28 changes: 28 additions & 0 deletions LeetCode/May/16-5-24/Solution.java
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit 3041691

Please sign in to comment.