Skip to content

Instantly share code, notes, and snippets.

@zhoufenfens
Created March 30, 2019 16:45
Show Gist options
  • Save zhoufenfens/f8a9e82a4b6e23c54c78dd360233fe26 to your computer and use it in GitHub Desktop.
Save zhoufenfens/f8a9e82a4b6e23c54c78dd360233fe26 to your computer and use it in GitHub Desktop.
二叉树bfs
void PrintNodeByLevel(Node *root)
{
int parentSize = 1, childSize = 0;
Node * temp;
queue<Node *> q;
q.push(root);
do
{
temp = q.front();
cout << temp->data << " ";
q.pop();
if (temp->pLeft != NULL)
{
q.push(temp->pLeft);
childSize ++;
}
if (temp->pRight != NULL)
{
q.push(temp->pRight);
childSize ++;
}
parentSize--;
if (parentSize == 0)
{
parentSize = childSize;
childSize = 0;
cout << endl;
}
} while (!q.empty());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment