二叉树的层序遍历
来源:互联网 发布:剑三淘宝充值 编辑:程序博客网 时间:2024/05/22 17:19
关于层序遍历的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;elsecout << " ";qnode.pop(); }count = tempCount;start = 0;cout << endl;}}
- 二叉树的层序遍历
- 二叉树的层序遍历
- 二叉树的层序遍历
- 二叉树的层序遍历
- 二叉树的层序遍历
- 二叉树的层序遍历
- 二叉树的层序遍历
- 二叉树的层序遍历
- 二叉树的层序遍历
- 二叉树的层序遍历
- 二叉树的层序遍历
- 二叉查找树的层序遍历
- 二叉树的层序遍历
- SDUTACM 二叉树的层序遍历
- 二叉树的层序遍历
- 二叉树的层序遍历
- Leetcode038--二叉树的层序遍历
- 数据结构 二叉树的层序遍历
- Linux SSH远程文件/目录传输命令scp
- 各种输入问题
- obj.style.width
- 捕食者物体跟踪算法:未来的计算机操作界面
- 南京一月
- 二叉树的层序遍历
- 未能启用约束。一行或多行中包含违反非空、唯一或外键约束的值
- altium designer板层定义介绍
- 利用js将html表格导出为excel文件
- linux gdb配合core文件进行强强联手
- ——
- asp导出到excel
- 23岁我写了第一封情书
- Tracing Tips and Resources