-
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
ea9cdff
commit 56f77e7
Showing
4 changed files
with
128 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,107 @@ | ||
//{ Driver Code Starts | ||
import java.util.*; | ||
|
||
class Node { | ||
int data; | ||
Node left, right; | ||
|
||
Node(int val) { | ||
data = val; | ||
left = right = null; | ||
} | ||
} | ||
|
||
public class GFG | ||
{ | ||
public static void printList(Node head) { | ||
while (head != null) { | ||
if (head.left != null) | ||
System.out.print("-1 "); | ||
System.out.print(head.data + " "); | ||
head = head.right; | ||
} | ||
System.out.println(); | ||
} | ||
|
||
public static void main(String[] args) { | ||
Scanner sc = new Scanner(System.in); | ||
int t = sc.nextInt(); | ||
sc.nextLine(); // Consume newline | ||
while (t-- > 0) { | ||
String str = sc.nextLine(); | ||
Node root = buildTree(str); | ||
Solution ob = new Solution(); | ||
Node ans = ob.flattenBST(root); | ||
printList(ans); | ||
} | ||
sc.close(); | ||
} | ||
|
||
private static Node buildTree(String str) { | ||
if (str.length() == 0 || str.charAt(0) == 'N') | ||
return null; | ||
|
||
String[] ip = str.split("\\s+"); | ||
int index = 0; | ||
|
||
Node root = new Node(Integer.parseInt(ip[index++])); | ||
Queue<Node> queue = new LinkedList<>(); | ||
queue.add(root); | ||
|
||
while (!queue.isEmpty() && index < ip.length) { | ||
Node currNode = queue.poll(); | ||
|
||
String currVal = ip[index++]; | ||
if (!currVal.equals("N")) { | ||
currNode.left = new Node(Integer.parseInt(currVal)); | ||
queue.add(currNode.left); | ||
} | ||
|
||
if (index >= ip.length) | ||
break; | ||
currVal = ip[index++]; | ||
if (!currVal.equals("N")) { | ||
currNode.right = new Node(Integer.parseInt(currVal)); | ||
queue.add(currNode.right); | ||
} | ||
} | ||
|
||
return root; | ||
} | ||
} | ||
|
||
// } Driver Code Ends | ||
|
||
|
||
//User function Template for Java | ||
class Solution | ||
{ | ||
static Node prev; | ||
|
||
public Node flattenBST(Node root) | ||
{ | ||
// Code here | ||
Node dummy = new Node(-1); | ||
prev = dummy; | ||
|
||
Inorder(root); | ||
|
||
prev.left = null; | ||
prev.right = null; | ||
Node ret = dummy.right; | ||
|
||
return ret; | ||
} | ||
|
||
private void Inorder(Node curr) | ||
{ | ||
if (curr == null) | ||
return; | ||
|
||
Inorder(curr.left); | ||
prev.left = null; | ||
prev.right = curr; | ||
prev = curr; | ||
Inorder(curr.right); | ||
} | ||
} |
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(n) | ||
Space complexity - O(n) |
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(n*logn) | ||
Space complexity - O(n) |
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,17 @@ | ||
class Solution | ||
{ | ||
public int findLeastNumOfUniqueInts(int[] arr, int k) | ||
{ | ||
Map<Integer, Integer> count = new HashMap<>(); | ||
|
||
for (int a : arr) | ||
count.merge(a, 1, Integer::sum); | ||
|
||
Queue<Integer> minHeap = new PriorityQueue<>(count.values()); | ||
|
||
while (k > 0) | ||
k -= minHeap.poll(); | ||
|
||
return minHeap.size() + (k < 0 ? 1 : 0); | ||
} | ||
} |