Skip to content

Commit

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




//Initial Template for Java

//Contributed by Sudarshan Sharma
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;
}
void inOrder(Node node) {
if (node == null) {
return;
}

inOrder(node.left);
System.out.print(node.data + " ");

inOrder(node.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();
System.out.println(g.isSumProperty(root));

}
}
}


// } Driver Code Ends


//User function Template for Java


/*Complete the function below
Node is as follows:
class Node{
int data;
Node left,right;
Node(int key)
{
data = key;
left = right = null;
}
}
*/
class Solution
{
//Function to check whether all nodes of a tree have the value
//equal to the sum of their child nodes.
public static int isSumProperty(Node root)
{
// add your code here
Queue<Node> q = new LinkedList<Node>();

q.offer(root);
q.offer(null);

while (!q.isEmpty())
{
Node curr = q.poll();

if (curr == null)
{
if (!q.isEmpty())
q.offer(null);
continue;
}

int sum = 0;

if (curr.left != null)
{
q.offer(curr.left);
sum += curr.left.data;
}

if (curr.right != null)
{
q.offer(curr.right);
sum += curr.right.data;
}

if (sum != curr.data && sum != 0)
return 0;
}

return 1;
}
}
2 changes: 2 additions & 0 deletions GeeksForGeeks/February/9-2-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)
2 changes: 2 additions & 0 deletions LeetCode/February/9-2-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(n^2)
Space complexity - O(n)
41 changes: 41 additions & 0 deletions LeetCode/February/9-2-24/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
class Solution
{
public List<Integer> largestDivisibleSubset(int[] nums)
{
final int n = nums.length;
List<Integer> ans = new ArrayList<>();

int[] sizeEndsAt = new int[n];
int[] prevIndex = new int[n];
int maxSize = 0;
int index = -1;

Arrays.fill(sizeEndsAt, 1);
Arrays.fill(prevIndex, -1);
Arrays.sort(nums);

for (int i = 0; i < n; ++i)
{
for (int j = i - 1; j >= 0; --j)
if (nums[i] % nums[j] == 0 && sizeEndsAt[i] < sizeEndsAt[j] + 1)
{
sizeEndsAt[i] = sizeEndsAt[j] + 1;
prevIndex[i] = j;
}

if (maxSize < sizeEndsAt[i])
{
maxSize = sizeEndsAt[i];
index = i;
}
}

while (index != -1)
{
ans.add(nums[index]);
index = prevIndex[index];
}

return ans;
}
}

0 comments on commit f3c0a3b

Please sign in to comment.