parvin.singh
BAN USERit seems here we are trying to read the input as a pre-order traversal of a binary search tree. This is not mentioned in the original question ?
- parvin.singh October 26, 2015A little deviation from the above approach, I am also using recursion to link the nodes at the same level.
Approach is to use recursion to reach the desired level and if the left child and right child both are not null then running through the next pointers of the left child until we reach the end and then linking it with the right child.
Returning left child as long as it is not null otherwise we return the right child.
public void associateNextPtr(){
// need to associate pointers starting from the root
int level = 2;
while(associateNextPtrAtLevel(root,level)!=null){
level++;
}
System.out.println("total levels are: " + level-1);
}
private Node associateNextPtrAtLevel(Node root, int level) {
Node retVal;
if (root == null) {
retVal = null;
} else if (level == 1) {
retVal = root;
} else {
Node leftChild = associateNextPtrAtLevel(root.left, level - 1);
Node rightChild = associateNextPtrAtLevel(root.right, level - 1);
if (leftChild != null && rightChild != null) {
Node scanNode = leftChild;
while(scanNode.nextNode != null){
scanNode = scanNode.nextNode;
}
scanNode.nextNode = rightChild;
retVal = leftChild;
} else if (leftChild != null) {
retVal = leftChild;
} else {
retVal = rightChild;
}
}
return retVal;
}
This is answering the minimum number of mediators required for the communication. The question is to find the maximum mediators involved in the talk of 2 team owners.
- parvin.singh October 26, 2015