KY11 二叉树遍历

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();
        }
    }
}

  • 11
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

数九天有一个秘密

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值