1.题目:
2.我的代码:
C语言:
#include <stdio.h>
#include <stdlib.h>
typedef struct BinaryTreeNode
{
char data;
struct BinaryTreeNode* left;
struct BinaryTreeNode* right;
}BTNode;
BTNode* BinaryTreeCreat(BTNode* root, char str[], int* pi)
{
if (str[*pi] != '#')
{
root = (BTNode*)malloc(sizeof(BTNode));
root->data = str[*pi];
(*pi)++;
root->left = BinaryTreeCreat(root->left, str, pi);
(*pi)++;
root->right = BinaryTreeCreat(root->right, str, pi);
return root;
}
return NULL;
}
void BinaryTreeInOrder(BTNode* root)
{
if (root == NULL)
{
return;
}
BinaryTreeInOrder(root->left);
printf("%c ", root->data);
BinaryTreeInOrder(root->right);
}
int main()
{
char str[101] = { 0 };
scanf("%s", str);
BTNode* root;
int i = 0;
root = BinaryTreeCreat(root, str, &i);
BinaryTreeInOrder(root);
return 0;
}
Java:
import java.util.Scanner;
class TreeNode {
char val;
TreeNode left;
TreeNode right;
TreeNode(char val) {
this.val = val;
}
}
public class Main {
public static int i;
public static TreeNode createTree(String str) {
TreeNode root = null;
if (str.charAt(i) != '#') {
root = new TreeNode(str.charAt(i));
++i;
root.left = createTree(str);
root.right = createTree(str);
} else {
++i;
}
return root;
}
public static void inOrder(TreeNode root) {
if (root == null) {
return;
}
inOrder(root.left);
System.out.print(root.val + " ");
inOrder(root.right);
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String str = scanner.nextLine();
TreeNode root = createTree(str);
inOrder(root);
}
}
3.答案代码:
C语言:
/*
解题思路:
在先序遍历的过程中构建每一个节点
*/
#include <stdio.h>
#include <malloc.h>
typedef struct BTNode
{
char _data;
struct BTNode* _left;
struct BTNode* _right;
}BTNode;
//中序遍历
void Inorder(BTNode* root)
{
if(root)
{
Inorder(root->_left);
printf("%c ", root->_data);
Inorder(root->_right);
}
}
BTNode* CreatBTree(char* str, int* pi)
{
if(str[*pi]!= '#')
{
//当前节点非空,则创建当前节点
BTNode*root=(BTNode*)malloc(sizeof(BTNode));
root->_data = str[*pi];
//字符位置向后移动一个位置
++(*pi);
//创建左子树
root->_left=CreatBTree(str,pi);
//字符位置向后移动一个位置
++(*pi);
//创建右子树
root->_right=CreatBTree(str,pi);
return root;
}
else
return NULL; //如果是空节点,则返回NULL
}
int main()
{
char str[101];
int i = 0;
//读入字符串
scanf("%s", str);
//创建二叉树
BTNode* root = CreatBTree(str, &i);
//中序打印二叉树
Inorder(root);
printf("\n");
return 0;
}
Java:
/*
注意在线OJ算法题分为两种类型:
1. 接口类型OJ:此种OJ题目是方法名字已经给好,用户直接写代码即可,也不需要包含什么包
2. IO类型的OJ:此种OJ题目需要用户定义一个public的Main类,然后在Main类中提供一个main方法,在main方法中完成事情,中间如要需要用到其他集合类,必须手动
导入包
在线OJ中的循环输入,输入单个值怎么循环接收,整行值怎么循环接收
解题思路:
参考课堂将的二叉树的创建以及遍历
*/
import java.util.*;
public class Main{
// 二叉树的节点进行定义
public static class TreeNode{
char value;
TreeNode left;
TreeNode right;
public TreeNode( char value){
this.value = value;
}
}
// 指向二叉树的根节点
TreeNode root;
int index;
void createBinaryTree(String preStr, char invalid){
index = 0;
root = createBinaryTreeN(preStr, invalid);
}
TreeNode createBinaryTreeN(String preStr, char invalid){
TreeNode treeRoot = null;
if(index < preStr.length() && preStr.charAt(index) != invalid){
// 创建根节点
treeRoot = new TreeNode(preStr.charAt(index));
// 创建根节点的左子树
++index;
treeRoot.left = createBinaryTreeN(preStr, invalid);
// 创建根节点的右子树
++index;
treeRoot.right = createBinaryTreeN(preStr, invalid);
}
return treeRoot;
}
public void InOrder(){
InOrder(root);
System.out.println();
}
private void InOrder(TreeNode treeRoot){
if(treeRoot != null){
InOrder(treeRoot.left);
System.out.print(treeRoot.value + " ");
InOrder(treeRoot.right);
}
}
public static void main(String[] args){
Scanner scaner = new Scanner(System.in);
while(scaner.hasNext()){
// 接收前序遍历的结果
String str = scaner.nextLine();
Main tree = new Main();
tree.createBinaryTree(str, '#');
tree.InOrder();
}
}
}