本文主要是介绍OJ_二叉树已知先序遍历序列(有空叶子)求中序遍历序列,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题干
C++实现
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<vector>
using namespace std;struct TreeNode
{char data;TreeNode* left;TreeNode* right;
};TreeNode* RecursiveBuildTree(int& i, char str[]) {char c = str[i];++i;if (c == '#') {return NULL;}else {TreeNode* pNew = new TreeNode();//堆中申请的变量,可以作为函数返回值pNew->data = c;pNew->left = RecursiveBuildTree(i, str);pNew->right = RecursiveBuildTree(i, str);return pNew;}
}void InOrder(TreeNode* proot) {if (proot == NULL) {return;}InOrder(proot->left);printf("%c ", proot->data);InOrder(proot->right);
}int main() {char str[1000];while (scanf("%s", str) != EOF) {int i = 0;TreeNode* proot = RecursiveBuildTree(i, str);InOrder(proot);printf("\n");}return 0;
}
这篇关于OJ_二叉树已知先序遍历序列(有空叶子)求中序遍历序列的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!