Created
April 22, 2018 17:07
-
-
Save Kishy-nivas/0b0a205c62fc77e89e5b7de4fdd1e194 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/*package whatever //do not write package name here */ | |
import java.util.*; | |
import java.lang.*; | |
import java.io.*; | |
class GFG { | |
private static void printLeafNodes(ArrayList < Integer > arr) { | |
if (arr.size() == 0) return; | |
if (arr.size() == 1) { | |
System.out.print(arr.get(0) + " "); | |
} | |
ArrayList < Integer > leftChild = new ArrayList < Integer > (); | |
ArrayList < Integer > rightChild = new ArrayList < Integer > (); | |
for (int i = 1; i < arr.size(); i++) { | |
if (arr.get(0) > arr.get(i)) | |
leftChild.add(arr.get(i)); | |
if (arr.get(0) < arr.get(i)) | |
rightChild.add(arr.get(i)); | |
} | |
printLeafNodes(leftChild); | |
printLeafNodes(rightChild); | |
} | |
public static void main(String[] args) { | |
//code | |
Scanner sc = new Scanner(System.in); | |
int t = sc.nextInt(); | |
while (t > 0) { | |
t--; | |
ArrayList < Integer > al = new ArrayList < Integer > (); | |
int n = sc.nextInt(); | |
for (int i = 0; i < n; i++) { | |
int val = sc.nextInt(); | |
al.add(val); | |
} | |
printLeafNodes(al); | |
System.out.println(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment