-
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
0b2ef58
commit 1057f80
Showing
4 changed files
with
124 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,102 @@ | ||
//{ Driver Code Starts | ||
import java.util.*; | ||
|
||
class Node { | ||
int data; | ||
Node left; | ||
Node right; | ||
|
||
Node(int value) { | ||
data = value; | ||
left = null; | ||
right = null; | ||
} | ||
} | ||
|
||
class InorderPostorderToTree { | ||
public void preOrder(Node root) { | ||
if (root == null) return; | ||
|
||
System.out.print(root.data + " "); | ||
preOrder(root.left); | ||
|
||
preOrder(root.right); | ||
} | ||
|
||
public static void main(String args[]) { | ||
Scanner sc = new Scanner(System.in); | ||
InorderPostorderToTree ip = new InorderPostorderToTree(); | ||
int T = sc.nextInt(); | ||
while (T > 0) { | ||
int n = sc.nextInt(); | ||
int inorder[] = new int[n]; | ||
int postorder[] = new int[n]; | ||
for (int i = 0; i < n; i++) inorder[i] = sc.nextInt(); | ||
for (int i = 0; i < n; i++) postorder[i] = sc.nextInt(); | ||
GfG g = new GfG(); | ||
Node root = g.buildTree(inorder, postorder, n); | ||
ip.preOrder(root); | ||
System.out.println(); | ||
|
||
T--; | ||
} | ||
} | ||
} | ||
|
||
// } Driver Code Ends | ||
|
||
|
||
/* Tree node structure | ||
class Node | ||
{ | ||
int data; | ||
Node left; | ||
Node right; | ||
Node(int value) | ||
{ | ||
data = value; | ||
left = null; | ||
right = null; | ||
} | ||
}*/ | ||
|
||
|
||
class GfG | ||
{ | ||
private static int findPos(int[] in, int inStart, int inEnd, int elem) | ||
{ | ||
for (int i = inStart; i <= inEnd; i++) | ||
{ | ||
if (in[i] == elem) | ||
{ | ||
return i; | ||
} | ||
} | ||
return -1; | ||
} | ||
|
||
private static Node solve(int[] in, int[] post, int[] postIdx, int inStart, int inEnd, int n) | ||
{ | ||
if (postIdx[0] < 0 || inStart > inEnd) | ||
{ | ||
return null; | ||
} | ||
|
||
int elem = post[postIdx[0]--]; | ||
Node root = new Node(elem); | ||
int pos = findPos(in, inStart, inEnd, elem); | ||
|
||
root.right = solve(in, post, postIdx, pos + 1, inEnd, n); | ||
root.left = solve(in, post, postIdx, inStart, pos - 1, n); | ||
|
||
return root; | ||
} | ||
|
||
// Function to return a tree created from postorder and inorder traversals. | ||
Node buildTree(int in[], int post[], int n) | ||
{ | ||
int[] postIdx = new int[]{n - 1}; | ||
return solve(in, post, postIdx, 0, n - 1, 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^2) | ||
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,4 @@ | ||
Time complexity - O(|sort|) | ||
Space complexity - O(|sort|) | ||
|
||
|sort| = n * logn |
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,16 @@ | ||
class Solution { | ||
public int numRescueBoats(int[] people, int limit) { | ||
int ans = 0; | ||
|
||
Arrays.sort(people); | ||
|
||
for (int i = 0, j = people.length - 1; i <= j; ++ans) | ||
{ | ||
int remain = limit - people[j--]; | ||
if (people[i] <= remain) | ||
++i; | ||
} | ||
|
||
return ans; | ||
} | ||
} |