This goal of this project is to analyze who benefits who in a pyramid scheme. PyramidScheme.java is a data structure which extends LinkedTree (LinkedTree.java is a very standard tree class), passing in Person (a Person.java is a participant in the pyramid scheme, class information is irrelevant) objects in place of the generic. 

The method in question that I cannot figure out is findNodeChain. This method, a member of PyramidScheme.java, is supposed to return an ArrayList of MultiTreeNode (MultiTreeNode.java is a very standard tree-node class) objects, being the people "who benifit" from the sucker - i.e., every node above the sucker. Here is the relevant code (findNodeChain is at the bottom):

```
 /**
 * Initiates the recursive findNodeChain method to return a list of all
 * nodes that are above the node containing the target. This overload takes
 * a Person.
 *
 * @param sucker the individual from whom others above are benefitting
 * @return an ArrayList of individuals higher up in the hierarchy
 */
public ArrayList<MultiTreeNode<Person>>
        whoBenefits(Person sucker) {
    return findNodeChain(root, sucker);
}

/**
 * Initiates the recursive findNodeChain method to return a list of all
 * nodes that are above the node containing the target. This overload takes
 * a MultiTreeNode.
 *
 * @param sucker the individual from whom others above are benefitting
 * @return an ArrayList of individuals higher up in the hierarchy
 */
public ArrayList<MultiTreeNode<Person>>
        whoBenefits(MultiTreeNode<Person> sucker) {
    return findNodeChain(root, sucker.getElement());
}

/**
 * Similar to findNode, but rather than returning the node that contains the
 * target, instead returns a list of nodes above the one that contains the
 * target, in low to high level order.
 *
 * @param node the current node being examined
 * @param target the Person being searched for
 * @return an ArrayList of nodes that are directly above the target in the
 * hierarchy
 */
private ArrayList<MultiTreeNode<Person>>
        findNodeChain(MultiTreeNode<Person> node, Person target) {
    return findNodeChain(node, target, new ArrayList<>());
}

/**
 * Primary helper method for findNodeChain
 *
 * @param node the current node being examined
 * @param target the Person being searched for
 * @param helper the ArrayList people to be returned
 * @return an ArrayList of nodes that are directly above the target in the
 * hierarchy
 */
private ArrayList<MultiTreeNode<Person>>
        findNodeChain(MultiTreeNode<Person> node, Person target, ArrayList<MultiTreeNode<Person>> helper) {
    if (node.getElement().equals(target)) { 
        // If this node is the target, add it to the list and return.
        helper.add(node); 
        return helper;
    } else {            
        //For each child of this node...
        for (MultiTreeNode<Person> child : node.getChildren()) {
            //...Call this method, seeing if the child can find our target.
            helper.add(child);
            helper = findNodeChain(child, target, helper);
            //If it isn't null, then the child has found our target, and...
            if (helper.get(helper.size() - 1) != null) {
                return helper; //...The target is passed upwards.
            }
        }
        // If none of the children have the target, return the passed ArrayList
        return helper;
    }
}

```

I started trying to solve this by basing findNodeChain() off the findNode() method in LinkedTree.java:

```
/**
 * Finds the node that contains a target element. Calls the recursive
 * overload to search for target.
 *
 * @param target the element being searched for
 * @return the MultiTreeNode containing the target, or null if not found
 */
public MultiTreeNode<T> findNode(T target) {
    if (target == null) {
        return null;
    }
    return findNode(root, target);
}

/**
 * Finds the node that contains a target element. This checks the current
 * node, and if it is the target, returns it. Otherwise, it recursively
 * checks each of the current node's children, to see if they can find it.
 * If none of this node's children can find it either, then null is
 * returned.
 *
 * @param node the node currently being examined
 * @param target the element being searched for
 * @return the MultiTreeNode containing the target, or null if not found
 */
private MultiTreeNode<T> findNode(MultiTreeNode<T> node, T target) {
    //If this node is the one holding the target...
    if (node.getElement().equals(target)) {
        return node; //...Return this node, so it is passed upwards.
    } else { //Otherwise...
        MultiTreeNode<T> temp;
        //For each child of this node...
        for (MultiTreeNode<T> child : node.getChildren()) {
            //...Call this method, seeing if the child can find our target.
            temp = findNode(child, target);
            //If it isn't null, then the child has found our target, and...
            if (temp != null) {
                return temp; //...The target is passed upwards.
            }
        }
        //If none of the children found the target, return null.
        return null; //This signifies that no target was found.
    }
}

```

Have also tried this:

```
/**
 * Similar to findNode, but rather than returning the node that contains the
 * target, instead returns a list of nodes above the one that contains the
 * target, in low to high level order.
 *
 * @param node   the current node being examined
 * @param target the Person being searched for
 * @return an ArrayList of nodes that are directly above the target in the
 * hierarchy
 */
private ArrayList<MultiTreeNode<Person>>
findNodeChain(MultiTreeNode<Person> node, Person target) {
    // Create the ArrayList here and pass its reference at each recursion
    ArrayList<MultiTreeNode<Person>> helper = new ArrayList<>();
    hasNodeChain(node, target, helper); // Pass the ArrayList
    return helper; // Return the passed around ArrayList. Yay for pass by reference!!

}

/**
 * Primary helper method for findNodeChain
 *
 * @param node   the current node being examined
 * @param target the Person being searched for
 * @param helper the pointer of the ArrayList of people to be returned
 * @return true if there is a path from node to target, false if not
 */
private boolean hasNodeChain(MultiTreeNode<Person> node, Person target, ArrayList<MultiTreeNode<Person>> helper) {
    helper.add(node); // Assume this node is a member of the list.
    if (node.getElement().equals(target)) { // If the node we're at equals the target, return it.
        return true;
    } else {
        for (MultiTreeNode<Person> child : node.getChildren()) { // For each child in the node, recurse the method.
            if (hasNodeChain(child, target, helper)) { // If a child has a path, this node is a member of the list.
                return true;
            }
        }
        // If none of the children are members, remove this node.
        helper.remove(helper.size() - 1);
        return false;
    }
}

```

I think that I am close to figuring this out, but not quite there. What I have just adds every element in the tree :(. Could anyone provide some guidance please?