Skip to content

Commit

Permalink
Added codes for 24 Jan
Browse files Browse the repository at this point in the history
  • Loading branch information
Tanmay-312 committed Jan 24, 2024
1 parent 8aaf102 commit 5b85515
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 0 deletions.
82 changes: 82 additions & 0 deletions GeeksForGeeks/24-1-24/GFG.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
//{ Driver Code Starts
import java.io.*;
import java.util.*;

public class GFG {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int t=scanner.nextInt();
while(t-- > 0)
{
int n = scanner.nextInt();
int m = scanner.nextInt();

ArrayList<ArrayList<Integer>> edges = new ArrayList<>();
for (int i = 0; i < m; i++) {
int u = scanner.nextInt();
int v = scanner.nextInt();
ArrayList<Integer> edge = new ArrayList<>();
edge.add(u);
edge.add(v);
edges.add(edge);
}

Solution solution = new Solution();
boolean result = solution.isTree(n, m, edges);

if (result==true) {
System.out.println(1);
} else {
System.out.println(0);
}
}
}
}
// } Driver Code Ends


//User function Template for Java
class Solution
{
public boolean isCyclePresent(int node,List<List<Integer>> adj,Set<Integer> vis,int p)
{
vis.add(node);
for(int i:adj.get(node))
{
if(!vis.contains(i))
{
if(!isCyclePresent(i,adj,vis,node))
return false;
}

else if(p!=i)
return false;
}

return true;
}


public boolean isTree(int n, int m, ArrayList<ArrayList<Integer>> edges)
{
// code here
List<List<Integer>> adj = new ArrayList<>();

for(int i=0;i<n;i++)
adj.add(new ArrayList<>());

for(ArrayList<Integer> e:edges)
{
adj.get(e.get(0)).add(e.get(1));
adj.get(e.get(1)).add(e.get(0));
}

Set<Integer> vis = new HashSet<>();
boolean b = isCyclePresent(0,adj,vis,-1);

if(vis.size()!=n)
return false;

return b;
}
}
2 changes: 2 additions & 0 deletions GeeksForGeeks/24-1-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(n+m)
Space complexity - O(n)
2 changes: 2 additions & 0 deletions LeetCode/24-1-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)
43 changes: 43 additions & 0 deletions LeetCode/24-1-24/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* 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 int pseudoPalindromicPaths (TreeNode root)
{
dfs(root,0);
return ans;
}

private int ans = 0;

private void dfs(TreeNode root, int path)
{
if (root == null)
return;

if (root.left == null && root.right == null)
{
path ^= 1 << root.val;
if ((path & (path - 1)) == 0)
++ans;

return;
}

dfs(root.left, path ^ 1 << root.val);
dfs(root.right, path ^ 1 << root.val);
}
}

0 comments on commit 5b85515

Please sign in to comment.