-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8aaf102
commit 5b85515
Showing
4 changed files
with
129 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Time complexity - O(n+m) | ||
Space complexity - O(n) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Time complexity - O(n) | ||
Space complexity - O(h) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |