Skip to content

Commit

Permalink
Added codes for 7 Feb
Browse files Browse the repository at this point in the history
  • Loading branch information
Tanmay-312 committed Feb 7, 2024
1 parent 8e385d2 commit 0e25e00
Show file tree
Hide file tree
Showing 4 changed files with 193 additions and 0 deletions.
155 changes: 155 additions & 0 deletions GeeksForGeeks/February/7-2-24/GFG.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
//{ Driver Code Starts
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 Main {
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) throws IOException {
BufferedReader br =
new BufferedReader(new InputStreamReader(System.in));

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

while (t-- > 0) {
String s = br.readLine().trim();
Node root = buildTree(s);
String[] ab = br.readLine().trim().split(" ");
int a = Integer.parseInt(ab[0]);
int b = Integer.parseInt(ab[1]);

GfG g = new GfG();
System.out.println(g.findDist(root, a, b));
}
}
}


// } Driver Code Ends


// FUNCTION CODE
/* A Binary Tree node
class Node
{
int data;
Node left, right;
Node(int item) {
data = item;
left = right = null;
}
} */

/* Should return minimum distance between a and b
in a tree with given root*/
class GfG
{
int findDist(Node root, int a, int b)
{
// Your code here
ans = 0;
_findDistance(root, a, b);
return ans;
}

public int ans;

public int _findDistance(Node root, int n1, int n2)
{
if (root == null)
return 0;

int left = _findDistance(root.left, n1, n2);
int right = _findDistance(root.right, n1, n2);

if (root.data == n1 || root.data == n2)
{
if (left != 0 || right != 0)
{
ans = Math.max(left, right);
return 0;
}
else
return 1;
}

else if (left != 0 && right != 0)
{
ans = left + right;
return 0;
}

else if (left != 0 || right != 0)
return Math.max(left, right) + 1;


return 0;
}
}
2 changes: 2 additions & 0 deletions GeeksForGeeks/February/7-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/7-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(n)
34 changes: 34 additions & 0 deletions LeetCode/February/7-2-24/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
class Solution
{
public String frequencySort(String s)
{
int n = s.length();
StringBuilder sb = new StringBuilder();
int[] count = new int[128];
// buckets[i] := characters that appear i times in s
List<Character>[] buckets = new List[n + 1];

for (final char c : s.toCharArray())
++count[c];

for (int i = 0; i < 128; ++i)
{
int freq = count[i];
if (freq > 0)
{
if (buckets[freq] == null)
buckets[freq] = new ArrayList<>();

buckets[freq].add((char) i);
}
}

for (int freq = n; freq > 0; --freq)
if (buckets[freq] != null)
for (final char c : buckets[freq])
for (int i = 0; i < freq; ++i)
sb.append(c);

return sb.toString();
}
}

0 comments on commit 0e25e00

Please sign in to comment.