-
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
0b8253c
commit faf5d53
Showing
5 changed files
with
204 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
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,170 @@ | ||
//{ Driver Code Starts | ||
import java.util.*; | ||
import java.io.*; | ||
|
||
class Node { | ||
int data; | ||
Node left; | ||
Node right; | ||
Node(int data) { | ||
this.data = data; | ||
left = null; | ||
right = null; | ||
} | ||
} | ||
|
||
public class GfG { | ||
public static Node buildTree(String str) { | ||
|
||
if (str.length() == 0 || str.charAt(0) == 'N') { | ||
return null; | ||
} | ||
|
||
String ip[] = str.split(" "); | ||
// Create the root of the tree | ||
Node root = new Node(Integer.parseInt(ip[0])); | ||
// Push the root to the queue | ||
|
||
Queue<Node> queue = new LinkedList<>(); | ||
|
||
queue.add(root); | ||
// Starting from the second element | ||
|
||
int i = 1; | ||
while (queue.size() > 0 && i < ip.length) { | ||
|
||
// Get and remove the front of the queue | ||
Node currNode = queue.peek(); | ||
queue.remove(); | ||
|
||
// Get the current node's value from the string | ||
String currVal = ip[i]; | ||
|
||
// If the left child is not null | ||
if (!currVal.equals("N")) { | ||
|
||
// Create the left child for the current node | ||
currNode.left = new Node(Integer.parseInt(currVal)); | ||
// Push it to the queue | ||
queue.add(currNode.left); | ||
} | ||
|
||
// For the right child | ||
i++; | ||
if (i >= ip.length) break; | ||
|
||
currVal = ip[i]; | ||
|
||
// If the right child is not null | ||
if (!currVal.equals("N")) { | ||
|
||
// Create the right child for the current node | ||
currNode.right = new Node(Integer.parseInt(currVal)); | ||
|
||
// Push it to the queue | ||
queue.add(currNode.right); | ||
} | ||
i++; | ||
} | ||
|
||
return root; | ||
} | ||
|
||
|
||
public static void main(String[] args) | ||
{ | ||
Scanner scn=new Scanner(System.in); | ||
|
||
int t = Integer.parseInt(scn.nextLine()); | ||
|
||
while (t-- > 0) | ||
{ | ||
int s = Integer.parseInt(scn.nextLine()); | ||
// sc.nextLine(); | ||
String res = scn.nextLine(); | ||
// sc.nextLine(); | ||
Node root = buildTree(res); | ||
|
||
Solution ob=new Solution(); | ||
ArrayList<ArrayList<Integer>> ans= ob.printPaths(root, s); | ||
|
||
Collections.sort(ans, new ElementWiseComparator()); | ||
|
||
for(int i=0;i<ans.size();i++) | ||
{ | ||
for(int j=0;j<ans.get(i).size();j++) | ||
{ | ||
System.out.print(ans.get(i).get(j) + " "); | ||
} | ||
System.out.println(" "); | ||
} | ||
} | ||
} | ||
|
||
static class ElementWiseComparator implements Comparator<ArrayList<Integer>> { | ||
@Override | ||
public int compare(ArrayList<Integer> list1, ArrayList<Integer> list2) { | ||
int minLength = Math.min(list1.size(), list2.size()); | ||
|
||
for (int i = 0; i < minLength; i++) { | ||
int comparison = Integer.compare(list1.get(i), list2.get(i)); | ||
if (comparison != 0) { | ||
return comparison; | ||
} | ||
} | ||
|
||
return Integer.compare(list1.size(), list2.size()); | ||
} | ||
} | ||
} | ||
// } Driver Code Ends | ||
|
||
|
||
//User function Template for Java | ||
|
||
/*Tree Node | ||
class Node { | ||
int data; | ||
Node left; | ||
Node right; | ||
Node(int data) { | ||
this.data = data; | ||
left = null; | ||
right = null; | ||
} | ||
} | ||
*/ | ||
|
||
class Solution | ||
{ | ||
public static ArrayList<ArrayList<Integer>> printPaths(Node root, int sum) | ||
{ | ||
// code here | ||
ArrayList<ArrayList<Integer>> ans =new ArrayList<>(); | ||
solve(root, 0, sum, new ArrayList<Integer>(), ans); | ||
return ans; | ||
} | ||
|
||
public static void solve(Node rt, int cur, int sum, ArrayList<Integer> a, ArrayList<ArrayList<Integer>> ans){ | ||
if(rt==null) | ||
return; | ||
|
||
cur+=rt.data; | ||
a.add(rt.data); | ||
|
||
if(cur==sum) | ||
{ | ||
ArrayList<Integer> res = new ArrayList<>(); | ||
|
||
for(int k: a) | ||
res.add(k); | ||
|
||
ans.add(res); | ||
} | ||
|
||
solve(rt.left, cur, sum, a, ans); | ||
solve(rt.right, cur,sum,a,ans); | ||
|
||
a.remove(a.size()-1); | ||
} | ||
} |
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^2) | ||
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(1) |
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,30 @@ | ||
class Solution { | ||
public int[] findErrorNums(int[] nums) | ||
{ | ||
int i=0; | ||
while(i<nums.length) | ||
{ | ||
int correct = nums[i]-1; | ||
if(i < nums.length && nums[i] != nums[correct]) | ||
swap(nums, i, correct); | ||
else | ||
i++; | ||
} | ||
|
||
for (int j=0; j<nums.length; j++) | ||
{ | ||
if (nums[j] != j+1) | ||
return new int[] {nums[j], j+1}; | ||
} | ||
|
||
return new int[] {-1,-1}; | ||
} | ||
|
||
|
||
public void swap(int[] arr, int first, int second) | ||
{ | ||
int temp = arr[first]; | ||
arr[first] = arr[second]; | ||
arr[second] = temp; | ||
} | ||
} |