Skip to content

Instantly share code, notes, and snippets.

@zeux
Created November 26, 2013 23:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save zeux/7668396 to your computer and use it in GitHub Desktop.
Save zeux/7668396 to your computer and use it in GitHub Desktop.
struct Node
{
Node* child;
};
void gatherDescendants(std::vector<Node*>& target, Node* root)
{
if (root->child)
{
target.push_back(root->child);
gatherDescendants(target, root->child);
}
}
void process(Node* root)
{
// Gather all descendants
std::vector<Node*> nodes;
gatherDescendants(nodes, root);
// Process all descendants
for (size_t i = 0; i < nodes.size(); ++i)
process(nodes[i]);
}
void processN(int count)
{
Node* root = 0;
for (int i = 0; i < count; ++i)
{
Node* newroot = new Node();
newroot->child = root;
root = newroot;
}
process(root);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment