getprith
BAN USER- 0of 0 votes
AnswersWrite a program to check if one tree is a subtree of other or not.
- getprith in India| Report Duplicate | Flag | PURGE
Applications Developer Trees and Graphs
i wasn't able to think of anything in the interview room but after coming out i realized.
{
public class BinaryTree {
Integer content;
BinaryTree left;
BinaryTree right;
@Override
public String toString() {
// program to return a string representation of this tree;
return content + ((left == null) ? "" : left.toString())
+ ((right == null) ? "" : right.toString());
}
}
}
if you toString both Trees you can just check if one is a substring of the other
Integer[] a = { 0, 3, 4, 5, 6, 7, 8, 12,19, 51 };
Integer[] b = { 4, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 58 };
List<Integer> finalArray = new ArrayList(Arrays.asList(b));
finalArray.add(0, Integer.MIN_VALUE);
finalArray.add(finalArray.size(), Integer.MAX_VALUE);
int progress = 0;
for (int i = 0; progress < a.length; i++) {
if (a[progress] < finalArray.get(i)) {
finalArray.add(i, a[progress++]);
}
}
finalArray.remove(0);
finalArray.remove(finalArray.size()-1);
any good?
- getprith June 11, 2013i came up with this but your's is so much more elegant.
private static void rearrangeArray(Integer[] intarry) {
// count the number of evens
int count = 0;
for (int i : intarry) {
if (i % 2 == 0)
count++;
}
int i = 0, j = intarry.length - 1;
while (true) {
if (i >= count) {
break;
}
while (intarry[i] % 2 == 0) {
i++;
}
while (intarry[j] % 2 != 0) {
j--;
}
exchangeValues(intarry, i, j);
i++;
j--;
}
}
the tree initially was not a tree. but looking at me struggle the guy made it a binary tree. can we see some code with your solution implemented?
- getprith June 24, 2013