tushargoel86
BAN USERHere i am pasting recursion code. My thoughts are as below:
1) If sum is less than root then both the numbers will be left of the BST without root.
2) If sum is equals to the root then numbers will be at the root including root.
3) If sum is greater than root then there are 3 cases: a) Both are at right side b) Both at left side c) One is in left side and other is at side.
After this decision we subtract the node value with the given sum and check whether the difference value present or not. Here is working code:
private Integer sum(LeftLeaningRedBlackTree<Integer, Integer>.Node node,
Integer number) {
if (node == null) {
return null;
}
int cmp = number.compareTo(node.key);
int nodeInt = node.key.intValue();
int userInt = number.intValue();
if (cmp < 0) {
if (node.left == null) {
return null;
}
nodeInt = node.left.key.intValue();
int diff = nodeInt - userInt;
Integer found = rbt.search(node.left, diff);
if (found == null) {
return sum(node.left, number);
} else {
System.out.println(nodeInt + " " + diff);
return 0;
}
} else if (cmp > 0) {
int diff = userInt - nodeInt;
boolean found = rbt.search(diff);
if (!found || diff == nodeInt) {
return sum(node.right, number);
} else {
System.out.println(nodeInt + " " + diff);
return 0;
}
} else {
int diff = userInt - nodeInt;
boolean found = rbt.search(diff);
if (!found) {
return sum(node.right, number);
} else {
System.out.println(nodeInt + " " + diff);
return 0;
}
}
}
your approach is good. But you have used only for 10 task. For 100 task we have to submit 100 task and may be used with 10 or more threads. Right?
- tushargoel86 September 27, 2015