Skip to content

Commit

Permalink
Added codes for 3 April
Browse files Browse the repository at this point in the history
  • Loading branch information
Tanmay-312 committed Apr 3, 2024
1 parent 856cc77 commit 4ac2edb
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 0 deletions.
114 changes: 114 additions & 0 deletions GeeksForGeeks/April/03-4-24/GFG.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
//{ Driver Code Starts
import java.io.*;
import java.math.*;
import java.util.*;

class Node {
int data;
Node left, right;

public Node(int d) {
data = d;
left = right = null;
}
}

public class GFG {
static Node buildTree(String str) {
// Corner Case
if (str.length() == 0 || str.equals('N')) return null;
String[] s = str.split(" ");

Node root = new Node(Integer.parseInt(s[0]));
Queue<Node> q = new LinkedList<Node>();
q.add(root);

// Starting from the second element
int i = 1;
while (!q.isEmpty() && i < s.length) {
// Get and remove the front of the queue
Node currNode = q.remove();

// Get the curr node's value from the string
String currVal = s[i];

// If the left child is not null
if (!currVal.equals("N")) {

// Create the left child for the curr node
currNode.left = new Node(Integer.parseInt(currVal));

// Push it to the queue
q.add(currNode.left);
}

// For the right child
i++;
if (i >= s.length) break;
currVal = s[i];

// If the right child is not null
if (!currVal.equals("N")) {

// Create the right child for the curr node
currNode.right = new Node(Integer.parseInt(currVal));

// Push it to the queue
q.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[] snums = br.readLine().split(" ");
int k = Integer.parseInt(snums[0]);
int x = Integer.parseInt(snums[1]);
int y = Integer.parseInt(snums[2]);

Solution T = new Solution();
// Call the function with the needed parameters
System.out.println(T.kthCommonAncestor(root, k, x, y));

t--;
}
}
}

// } Driver Code Ends


// User function Template for Java
class Solution
{
public int kthCommonAncestor(Node root, int k, int x, int y)
{
// code here
List<Integer> ans = new ArrayList<>();
Node node = root;

while (node != null)
{
ans.add(node.data);

if (node.data < x && node.data < y)
node = node.right;
else if (node.data > x && node.data > y)
node = node.left;
else
break;
}

return (ans.size()-k >= 0) ? ans.get(ans.size()-k) : -1;
}
}
2 changes: 2 additions & 0 deletions GeeksForGeeks/April/03-4-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(h)
Space complexity - O(h)
2 changes: 2 additions & 0 deletions LeetCode/April/03-4-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(m*n*4^|words|)
Space complexity - O(4^|words|)
34 changes: 34 additions & 0 deletions LeetCode/April/03-4-24/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
class Solution
{
public boolean exist(char[][] board, String word)
{
for (int i = 0; i < board.length; ++i)
for (int j = 0; j < board[0].length; ++j)
if (dfs(board, word, i, j, 0))
return true;

return false;
}

private boolean dfs(char[][] board, String word, int i, int j, int s)
{
if (i < 0 || i == board.length || j < 0 || j == board[0].length)
return false;

if (board[i][j] != word.charAt(s) || board[i][j] == '*')
return false;

if (s == word.length() - 1)
return true;

char cache = board[i][j];
board[i][j] = '*';
boolean isExist = dfs(board, word, i + 1, j, s + 1) ||
dfs(board, word, i - 1, j, s + 1) ||
dfs(board, word, i, j + 1, s + 1) ||
dfs(board, word, i, j - 1, s + 1);

board[i][j] = cache;
return isExist;
}
}

0 comments on commit 4ac2edb

Please sign in to comment.