Suppose you are given the root node Node root; of a binary tree. How would you write an iterator for the tree?
(Note that each node node has two attributes: node.left and node.right, the left and right children of the node, respectively.)
Traversing it recursively:
1 2 3 4 5 6 7 8
|
void iterate(Node* node)
{
if(node==NULL)
return;
do_something_with_the_node(node);
iterate(node->left);
iterate(node->right);
}
|
Last edited on