Skip to content

Instantly share code, notes, and snippets.

@PIlin
Forked from zeux/gist:7668396
Last active December 29, 2015 12:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PIlin/7671239 to your computer and use it in GitHub Desktop.
Save PIlin/7671239 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);
// цикл выполнится count-1 раз
for (Node* r = root; r->child; r = r->child)
{
nodes.push_back(r->child);
}
// Process all descendants
// цикл выполнится count раз
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