-
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
3f6edd9
commit 137e572
Showing
4 changed files
with
231 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,198 @@ | ||
//{ Driver Code Starts | ||
import java.io.*; | ||
import java.util.*; | ||
|
||
|
||
class Node | ||
{ | ||
int data; | ||
Node left; | ||
Node right; | ||
|
||
Node(int data) | ||
{ | ||
this.data = data; | ||
left = null; | ||
right = null; | ||
} | ||
|
||
public static Node buildTree(String str) | ||
{ | ||
// Corner Case | ||
if(str.length()==0 || str.charAt(0)=='N') | ||
return null; | ||
|
||
// Creating array of Strings from input | ||
// String after spliting by space | ||
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 Node inputTree(BufferedReader br) throws IOException | ||
{ | ||
return buildTree(br.readLine().trim()); | ||
} | ||
|
||
public static void inorder(Node root) | ||
{ | ||
if (root == null) | ||
return; | ||
inorder(root.left); | ||
System.out.print(root.data + " "); | ||
inorder(root.right); | ||
} | ||
} | ||
|
||
|
||
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){ | ||
|
||
Node root = Node.inputTree(br); | ||
|
||
Solution obj = new Solution(); | ||
ArrayList<ArrayList<Integer>> res = obj.Paths(root); | ||
|
||
IntMatrix.print(res); | ||
|
||
} | ||
} | ||
} | ||
|
||
// } Driver Code Ends | ||
|
||
|
||
/* | ||
Definition for Binary Tree Node | ||
class Node | ||
{ | ||
int data; | ||
Node left; | ||
Node right; | ||
Node(int data) | ||
{ | ||
this.data = data; | ||
left = null; | ||
right = null; | ||
} | ||
} | ||
*/ | ||
|
||
class Solution { | ||
private static void helper(Node root, ArrayList<Integer> temp, ArrayList<ArrayList<Integer>> ans) | ||
{ | ||
if(root==null) | ||
return; | ||
|
||
temp.add(root.data); | ||
|
||
if(root.left==null && root.right==null) | ||
ans.add(new ArrayList<>(temp)); | ||
|
||
helper(root.left, temp, ans); | ||
helper(root.right, temp, ans); | ||
|
||
temp.remove(temp.size()-1); | ||
} | ||
|
||
public static ArrayList<ArrayList<Integer>> Paths(Node root) | ||
{ | ||
// code here | ||
ArrayList<ArrayList<Integer>> ans = new ArrayList<>(); | ||
helper(root, new ArrayList<>(), ans); | ||
return ans; | ||
} | ||
} |
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,2 @@ | ||
Time complexity - O(n*logn) | ||
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,29 @@ | ||
class Solution | ||
{ | ||
public String[] findRelativeRanks(int[] score) | ||
{ | ||
int n = score.length; | ||
int[] sortedScore = score.clone(); | ||
Arrays.sort(sortedScore); | ||
String[] ranks = new String[n]; | ||
|
||
for (int i = 0; i < n; i++) | ||
{ | ||
int rank = Arrays.binarySearch(sortedScore, score[i]); | ||
if (rank == n - 1) { | ||
ranks[i] = "Gold Medal"; | ||
} | ||
else if (rank == n - 2) { | ||
ranks[i] = "Silver Medal"; | ||
} | ||
else if (rank == n - 3) { | ||
ranks[i] = "Bronze Medal"; | ||
} | ||
else { | ||
ranks[i] = String.valueOf(n - rank); | ||
} | ||
} | ||
|
||
return ranks; | ||
} | ||
} |