Created
March 30, 2019 16:45
-
-
Save zhoufenfens/f8a9e82a4b6e23c54c78dd360233fe26 to your computer and use it in GitHub Desktop.
二叉树bfs
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
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