-
-
Save PIlin/7671239 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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