There is a simple binary tree
struct Node { int x; Node *l, *r; }; It is necessary to display all its elements level by level. The question is how to implement it? It is desirable in the form of a code with detailed comments. Unfortunately, Google did not help me. I would be very grateful to someone who clearly explains.
Now there is a BFS function, but it remains to be asked how to get each level off a new line?
void LOPrint(Node *root) { if (root == NULL) { return; } queue<Node *> q; q.push(root); while (!q.empty()) { Node* temp = q.front(); q.pop(); cout << temp->x << " "; if (temp->l != NULL) q.push(temp->l); if (temp->r != NULL) q.push(temp->r); } cout << endl; }
BFSsomething. In Google there is. - pavel