Hawaiʻi's Technology Community

I recently solved this for a job interview. See how you do with this.

There exists in memory a tree of nodes.
The programming question is to write the body of the function 'countDescendents' shown below.  The example is shown in Java, but you can supply the solution in your programming language of choice.

The Node class is defined as:

public class Node {
 public Node firstChild;
 public Node nextSibling;

 public string name;

The function signature is:

public int countDescendents(Node node) {

For example, if a node was represented by a box with a right pointy end for nextSibling and a downward pointy end for firstChild, then a  family tree show might look like this:

if countDecendants was passed grandpa, it would return 12.

The countDescendents function should traverse the descendents of the node using only the firstChild and nextSibling attributes of Node.

Views: 39


web design, web development, localization

© 2024   Created by Daniel Leuck.   Powered by

Badges  |  Report an Issue  |  Terms of Service