Skip to content

Commit

Permalink
Added codes for 18 March
Browse files Browse the repository at this point in the history
  • Loading branch information
Tanmay-312 committed Mar 18, 2024
1 parent 1add395 commit 2fbe4c5
Show file tree
Hide file tree
Showing 4 changed files with 178 additions and 0 deletions.
150 changes: 150 additions & 0 deletions GeeksForGeeks/March/18-3-24/GFG.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
//{ Driver Code Starts
//Initial Template for Java

import java.util.LinkedList;
import java.util.Queue;
import java.io.*;
import java.util.*;

class Node{
int data;
Node left;
Node right;
Node(int data){
this.data = data;
left=null;
right=null;
}
}

class GfG {

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;
}
static void printInorder(Node root)
{
if(root == null)
return;

printInorder(root.left);
System.out.print(root.data+" ");

printInorder(root.right);
}

public static void main (String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

int t=Integer.parseInt(br.readLine());

while(t > 0){
String s = br.readLine();
Node root = buildTree(s);
Solution g = new Solution();
ArrayList <Integer> res = g.levelOrder(root);
for (Integer num : res) System.out.print(num + " ");
System.out.println();
t--;

}
}

}


// } Driver Code Ends


//User function Template for Java

/*
class Node
{
int data;
Node left, right;
Node(int item)
{
data = item;
left = right = null;
}
}
*/
class Solution
{
//Function to return the level order traversal of a tree.
static ArrayList <Integer> levelOrder(Node root)
{
// Your code here
Queue<Node> queue = new LinkedList<Node>();
ArrayList<Integer> ans = new ArrayList<>();
queue.add(root);
while (!queue.isEmpty())
{
Node tempNode = queue.poll();
ans.add(tempNode.data);

if (tempNode.left != null)
queue.add(tempNode.left);

if (tempNode.right != null)
queue.add(tempNode.right);
}

return ans;
}
}
2 changes: 2 additions & 0 deletions GeeksForGeeks/March/18-3-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/March/18-3-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(n * logn)
Space complexity - O(1)
24 changes: 24 additions & 0 deletions LeetCode/March/18-3-24/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class Solution
{
public int findMinArrowShots(int[][] points)
{
if (points == null || points.length == 0)
return 0;

Arrays.sort(points, (a, b) -> Integer.compare(a[1], b[1]));

int arrows = 1;
int end = points[0][1];

for (int i = 1; i < points.length; i++)
{
if (points[i][0] > end)
{
arrows++;
end = points[i][1];
}
}

return arrows;
}
}

0 comments on commit 2fbe4c5

Please sign in to comment.