原文链接:https://www.dreamwings.cn/ytu2346/2606.html

2346: 中序遍历二叉树

时间限制: 1 Sec  内存限制: 128 MB

提交: 12  解决: 3

题目描述

给定一颗二叉树,要求输出二叉树的深度以及中序遍历二叉树得到的序列。本题假设二叉树的结点数不超过1000。

输入

输入数据分为多组,第一行是测试数据的组数n,下面的n行分别代表一棵二叉树。每棵二叉树的结点均为正整数,数据为0代表当前结点为空,数据为-1代表二叉树数据输入结束,-1不作处理。二叉树的构造按照层次顺序(即第1层1个整数,第2层2个,第3层4个,第4层有8个......,如果某个结点不存在以0代替)

输出

输出每棵二叉树的深度以及中序遍历二叉树得到的序列。

样例输入

2
1 -1
1 2 0 3 4 -1

样例输出

1 1
3 3 2 4 1
思想:根据数据层序建立一棵二叉树,然后对其进行中序遍历即可~

代码:
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
using namespace std;
typedef struct Node             //定义二叉树
{
    int data;
    Node* lchild;
    Node* rchild;
} TBNode;
int depin;
void Init(TBNode *T)            //建立二叉树
{
    TBNode *a[105];
    TBNode *p=T;
    int real=0;
    while(cin>>p->data&&p->data!=-1)    //层序输入数据
    {
        a[++real]=p;                    //当前节点入队
        if(real/2!=0)                   //如果不是根节点,为当前节点父节点添加指针
        {
            if(real%2)a[real/2]->rchild=p;  //如果不能整除二说明是它父节点的右孩子
            else a[real/2]->lchild=p;       //否则为父节点左孩子
        }
        p->lchild=NULL;                 //当前节点孩子指针域设置为NULL
        p->rchild=NULL;
        p=(TBNode*)malloc(sizeof(TBNode));
    }
    depin=(int)ceil(log2(real+1));      //二叉树深度为所有节点个数加一 log2(real+1)向上取整
}
void Print(TBNode *T)               //先序输出二叉树
{
    if(T!=NULL)
    {
        Print(T->lchild);
        printf(T->data?" %d":"",T->data);
        Print(T->rchild);
    }
}
int main()
{
    int N;
    cin>>N;
    TBNode *Tree;                   //根节点
    Tree=(TBNode*)malloc(sizeof(TBNode));
    while(N--)
    {
        Init(Tree);                 //建立二叉树
        printf("%d",depin);         //输出深度
        Print(Tree);                //输出二叉树
        printf("\n");
    }
    return 0;
}

YTU 2346: 中序遍历二叉树的更多相关文章

  1. Leetcode 94. Binary Tree Inorder Traversal (中序遍历二叉树)

    Given a binary tree, return the inorder traversal of its nodes' values. For example: Given binary tr ...

  2. java创建二叉树并实现非递归中序遍历二叉树

    java创建二叉树并递归遍历二叉树前面已有讲解:http://www.cnblogs.com/lixiaolun/p/4658659.html. 在此基础上添加了非递归中序遍历二叉树: 二叉树类的代码 ...

  3. LeetCode OJ:Binary Tree Inorder Traversal(中序遍历二叉树)

    Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...

  4. Binary Tree Inorder Traversal-非递归实现中序遍历二叉树

    题目描述: 给定一颗二叉树,使用非递归方法实现二叉树的中序遍历 题目来源: http://oj.leetcode.com/problems/binary-tree-inorder-traversal/ ...

  5. LeetCode 94 | 基础题,如何不用递归中序遍历二叉树?

    今天是LeetCode专题第60篇文章,我们一起来看的是LeetCode的94题,二叉树的中序遍历. 这道题的官方难度是Medium,点赞3304,反对只有140,通过率有63.2%,在Medium的 ...

  6. YTU 2345: 后序遍历二叉树

    原文链接:https://www.dreamwings.cn/ytu2345/2611.html 2345: 后序遍历二叉树 时间限制: 1 Sec  内存限制: 128 MB 提交: 3  解决:  ...

  7. YTU 2344: 先序遍历二叉树

    原文链接:https://www.dreamwings.cn/ytu2344/2603.html 2344: 先序遍历二叉树 时间限制: 1 Sec  内存限制: 128 MB 提交: 4  解决:  ...

  8. 094 Binary Tree Inorder Traversal 中序遍历二叉树

    给定一个二叉树,返回其中序遍历.例如:给定二叉树 [1,null,2,3],   1    \     2    /   3返回 [1,3,2].说明: 递归算法很简单,你可以通过迭代算法完成吗?详见 ...

  9. java编写二叉树以及前序遍历、中序遍历和后序遍历 .

    /** * 实现二叉树的创建.前序遍历.中序遍历和后序遍历 **/ package DataStructure; /** * Copyright 2014 by Ruiqin Sun * All ri ...

随机推荐

  1. windows用户用VMware 虚拟机安装黑苹果Mac.OS.X操作系统

    使用的操作系统操作系统 windows7 SP1 X64 本教程所用 的软件的下载地址都在本教程中 ) 电脑内存低于 4G 的,加内存吧 … 1. SecurAble (检测你的 CPU 是否支持硬件 ...

  2. Log4J简单使用

    一.一般会将commons-logging和Log4j一起使用   原因:1.commons-logging功能较弱 2.log4j功能强大. 所需jar:       log4j-1.2.16.ja ...

  3. Go 语言学习

    golang中国 http://www.golangtc.com/ 第三方github学习 https://github.com/Unknwon/go-fundamental-programmingh ...

  4. Rails problem

    总是wa~ #include <stdio.h> int main() { ]; ], b[]; while(scanf("%d %s %s", &n, a, ...

  5. spidermark sensepostdata ntp_monlist.py

    试NTP 时间服务器用的,ntp_ip_enum.py,源码如下:#!/usr/bin/env python"""Basic script to pull address ...

  6. 找规律 ZOJ3498 Javabeans

    Javabeans are delicious. Javaman likes to eat javabeans very much. Javaman has n boxes of javabeans. ...

  7. Task示例,多线程

    class Program { static void Main(string[] args) { Run(); } public static async void Run() { var task ...

  8. bringSubviewToFront和insertSubview: atIndex:

    bring方法只能在当前SuperView中改变位置,insertSubview则可以脱离自己的superVIew,成为另个同级甚至高级的view的子view

  9. easyUI loyout tabs自适应宽度

    index.html 页面 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http ...

  10. fuelux.tree用法

    ACE中带了一个树,样式和操作挺好看的,就是难用,下面记录下如何使用. 首先fuelux.tree接受的数据源是Json,关键这个Json还不怎么标准,可接受的Json示例如下: { '刑侦': { ...