英伟达在线程序测评——后续遍历二叉树的序列转前序输出
来源:互联网 发布:淘宝被投诉低价竞争 编辑:程序博客网 时间:2024/05/21 08:51
题目:
题目比较简单吧,总之是利用栈先还原树,然后前序遍历即可
/*Sample code to read in test cases:*/#include <iostream>#include <fstream>#include <string>#include <cctype>#include <cassert>#include <stdlib.h>#include<vector>#include<stack>#include<string.h>#include<stdio.h>using namespace std;struct Node{vector<Node*> children;int flag;};void preTravese(Node* head,vector<int>* result){if(head==NULL)return;result->push_back(head->flag);int i;for(i=(head->children.size())-1;i>=0;i--)preTravese((head->children)[i],result);}int main(){fstream file;//char sysInputFile[] = "{sysFileUrl}";char sysInputFile[] = "C:\\Users\\jiangkai\\Desktop\\d.txt";file.open(sysInputFile);string lineBuffer;while (!file.eof()){ getline(file, lineBuffer); if (lineBuffer.length() == 0) continue; //ignore all empty lines else { //do something here stack<Node*> myStack; char* str = new char[lineBuffer.size()+1]; strcpy(str,lineBuffer.c_str()); char* element = strtok(str,","); while(element!=NULL) { int index = 0; int flag = 0; int count=0; while(element[index]!='(') { flag *= 10; flag += element[index]-'0'; index++; } index++; while(element[index]!=')') { count *= 10; count += element[index]-'0'; index++; } Node* newNode = new Node; newNode->flag = flag; if(count!=0) for(int k=0;k<count;k++) { Node* current = myStack.top(); myStack.pop(); (newNode->children).push_back(current); }myStack.push(newNode);element = strtok(NULL,","); } Node* head = myStack.top(); vector<int> result; preTravese(head,&result); int ii; cout<<result[0]; for(ii=1;ii<result.size();ii++) cout<<","<<result[ii]; cout<<endl; }}return 0;}
- 英伟达在线程序测评——后续遍历二叉树的序列转前序输出
- 二叉搜索树的后续遍历序列
- 二叉树的后续遍历序列
- 二叉搜索树的后续遍历序列
- 二叉搜索树的后续遍历序列
- 二叉搜索树的后续遍历序列
- 判断二叉树的后续遍历序列
- 二叉搜索树的后续遍历序列
- 搜索二叉树的后续遍历序列
- 二叉搜索树的后续遍历序列
- 二叉搜索树的后续遍历序列
- 剑指offer——二叉搜索树的后续遍历序列
- 剑指offer——二叉搜索树的后续遍历序列
- 已知一颗二叉树S的前序遍历和中序遍历 序列,请编程输出二叉树S的后续遍历序列.
- 已知一颗二叉树S的前序遍历和中序遍历序列,请编程输出二叉树S的后续遍历序列。
- 判断二叉搜索树的后续遍历序列
- 二叉查找树的后续遍历序列验证
- [剑指Offer]二叉搜索树的后续遍历序列
- Android SDK Manager无法更新的解决
- 给盒子的贺文
- javaweb项目中中文乱码
- 关于ListView中加入并选取checkbox错位的问题
- VS嵌入汇编ASM
- 英伟达在线程序测评——后续遍历二叉树的序列转前序输出
- POJ 2109 Power of Cryptography
- 11级_Java_曹建波 9.18 枚举&内省&beanutils
- 秋雨寄相思
- 使用C语言编写windows服务一般框架
- linux下屏保设置
- WebForm注册界面
- HDU 2577 How to Type
- paip.提升效率---源码生成流程图工具