为什么我的代码在VC中运行正常,在openjudge上却出现runtime error,求指导
题目是已知二叉树前序、中序求后序遍历。输入以EOF结束。代码如下:#include"stdafx.h"#include<iostream>#include<fstream...
题目是已知二叉树前序、中序求后序遍历。输入以EOF结束。
代码如下:
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
struct TreeNode
{
struct TreeNode* left;
struct TreeNode* right;
char elem;
};
void BinaryTreeFromOrderings(char* inorder, char* preorder, int length)
{
if(length == 0)
{
return;
}
TreeNode* node = new TreeNode;
node->elem = *preorder;
int rootIndex = 0;
for(;rootIndex < length; rootIndex++)
{
if(inorder[rootIndex] == *preorder)
break;
}
BinaryTreeFromOrderings(inorder, preorder +1, rootIndex);
BinaryTreeFromOrderings(inorder + rootIndex + 1, preorder + rootIndex + 1, length - (rootIndex + 1));
if(node->elem>0)
printf("%c",node->elem);
return;
}
int main(int argc, char* argv[])
{
char pr[1000];
char in[1000];
while(scanf("%s%s",pr,in)!=EOF)
{
BinaryTreeFromOrderings(in, pr, 8);
printf("\n");
}
return 0;
}
谢谢各位了! 展开
代码如下:
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
struct TreeNode
{
struct TreeNode* left;
struct TreeNode* right;
char elem;
};
void BinaryTreeFromOrderings(char* inorder, char* preorder, int length)
{
if(length == 0)
{
return;
}
TreeNode* node = new TreeNode;
node->elem = *preorder;
int rootIndex = 0;
for(;rootIndex < length; rootIndex++)
{
if(inorder[rootIndex] == *preorder)
break;
}
BinaryTreeFromOrderings(inorder, preorder +1, rootIndex);
BinaryTreeFromOrderings(inorder + rootIndex + 1, preorder + rootIndex + 1, length - (rootIndex + 1));
if(node->elem>0)
printf("%c",node->elem);
return;
}
int main(int argc, char* argv[])
{
char pr[1000];
char in[1000];
while(scanf("%s%s",pr,in)!=EOF)
{
BinaryTreeFromOrderings(in, pr, 8);
printf("\n");
}
return 0;
}
谢谢各位了! 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询