-
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
856cc77
commit 4ac2edb
Showing
4 changed files
with
152 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,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; | ||
} | ||
} |
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(h) | ||
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(m*n*4^|words|) | ||
Space complexity - O(4^|words|) |
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,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; | ||
} | ||
} |