关于层序遍历的STL代码如下,注意reserve的妙用,否则会因为vector容量增长导致跌代器失效:
下面代码不知为何总报错,伤感了,眼看找工作高峰来临,时间紧迫,扔这了。。。
void printLevel(Node *root)
{
if(root == NULL)
return;
vector<Node *> nvec;
nvec.reserve(100);
nvec.push_back(root);
vector<Node *>::iterator startIter = nvec.begin();
vector<Node *>::iterator lastIter = nvec.end();
while(startIter != lastIter) {
while(startIter != lastIter) {
cout << (*startIter)->data << " ";
Node *left = (*startIter)->left;
Node *right = (*startIter)->right;
if(left)
nvec.push_back(left);
if(right)
nvec.push_back(right);
++startIter;
}
cout << endl;
lastIter = nvec.end();
}
}
再次写二叉树的层序遍历,现在的代码可以说是简洁高效,窝稀饭
void printLevel(Node *root)
{
if (root == NULL)
return;
vector<Node *> nvec;
nvec.push_back(root);
int start = 0;
int end = 1;
int pos = end;
while (start != end)
{
Node *node = nvec[start];
cout << node->item << " ";
start++;
if (node->left)
{
nvec.push_back(node->left);
end++;
}
if (node->right)
{
nvec.push_back(node->right);
end++;
}
if (start == pos)
{
pos = end;
cout << endl;
}
}
}关于拓展问题:
访问顺序变为:
78
456
23
1
访问顺序变为:
87
654
32
1
用栈很容易实现~
void putNode2Stack(Node *root, stack<Node *> &snode)
{
if (root == NULL)
return;
vector<Node *> nvec;
nvec.push_back(root);
snode.push(root);
snode.push(NULL);
int start = 0;
int end = 1;
int pos = end;
while (start != end)
{
Node *node = nvec[start];
cout << node->item << " ";
start++;
if (node->left)
{
nvec.push_back(node->left);
snode.push(node->left);
end++;
}
if (node->right)
{
nvec.push_back(node->right);
snode.push(node->right);
end++;
}
if (start == pos)
{
pos = end;
snode.push(NULL);
cout << endl;
}
}
}
void putNode2Stack2(Node *root, stack<Node *> &snode)
{
if (root == NULL)
return;
vector<Node *> nvec;
nvec.push_back(root);
snode.push(root);
snode.push(NULL);
int start = 0;
int end = 1;
int pos = end;
while (start != end)
{
Node *node = nvec[start];
cout << node->item << " ";
start++;
if (node->right)
{
nvec.push_back(node->right);
snode.push(node->right);
end++;
}
if (node->left)
{
nvec.push_back(node->left);
snode.push(node->left);
end++;
}
if (start == pos)
{
pos = end;
snode.push(NULL);
cout << endl;
}
}
}利用queue来处理层序遍历
void printLevel(Node *root)
{
if (root == NULL)
return;
queue<Node *> nqueue;
nqueue.push(root);
int start = 0;
int last = 1;
int tempLast = last;
while (!nqueue.empty())
{
Node *top = nqueue.front();
nqueue.pop();
cout << top->data << " ";
if (top->left)
{
nqueue.push(top->left);
last++;
}
if (top->right)
{
nqueue.push(top->right);
last++;
}
start++;
if (start == tempLast)
{
tempLast = last;
cout << endl;
}
}
}
关于层序遍历,可以出现一个变种题目,那就是NULL指针打印成空格
void printLevel(Node *root)
{
if (root == NULL)
return;
queue<Node *> qnode;
qnode.push(root);
int count = 1;
int start = 0;
int tempCount = 0;
while (!qnode.empty())
{
tempCount = 0;
while (start != count)
{
Node *top = qnode.front();
if (top != NULL)
{
if (top->left)
{
qnode.push(top->left);
tempCount++;
}
else if (top->left == NULL && top->right != NULL)
{
qnode.push(NULL);
tempCount++;
}
if (top->right)
{
qnode.push(top->right);
tempCount++;
}
else if (top->left != NULL && top->right == NULL)
{
qnode.push(NULL);
tempCount++;
}
}
start++;
if (top != NULL)
cout << top->data;
else
cout << " ";
qnode.pop();
}
count = tempCount;
start = 0;
cout << endl;
}
}
二叉树层序遍历及优化算法
本文介绍了一种二叉树层序遍历的实现方式,并通过STL库优化了代码效率。同时提供了拓展问题及解决方案,包括访问顺序调整、使用栈和队列进行遍历等。
1万+

被折叠的 条评论
为什么被折叠?



