YTU 2346: 中序遍历二叉树
原文链接: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: 中序遍历二叉树的更多相关文章
- Leetcode 94. Binary Tree Inorder Traversal (中序遍历二叉树)
Given a binary tree, return the inorder traversal of its nodes' values. For example: Given binary tr ...
- java创建二叉树并实现非递归中序遍历二叉树
java创建二叉树并递归遍历二叉树前面已有讲解:http://www.cnblogs.com/lixiaolun/p/4658659.html. 在此基础上添加了非递归中序遍历二叉树: 二叉树类的代码 ...
- LeetCode OJ:Binary Tree Inorder Traversal(中序遍历二叉树)
Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...
- Binary Tree Inorder Traversal-非递归实现中序遍历二叉树
题目描述: 给定一颗二叉树,使用非递归方法实现二叉树的中序遍历 题目来源: http://oj.leetcode.com/problems/binary-tree-inorder-traversal/ ...
- LeetCode 94 | 基础题,如何不用递归中序遍历二叉树?
今天是LeetCode专题第60篇文章,我们一起来看的是LeetCode的94题,二叉树的中序遍历. 这道题的官方难度是Medium,点赞3304,反对只有140,通过率有63.2%,在Medium的 ...
- YTU 2345: 后序遍历二叉树
原文链接:https://www.dreamwings.cn/ytu2345/2611.html 2345: 后序遍历二叉树 时间限制: 1 Sec 内存限制: 128 MB 提交: 3 解决: ...
- YTU 2344: 先序遍历二叉树
原文链接:https://www.dreamwings.cn/ytu2344/2603.html 2344: 先序遍历二叉树 时间限制: 1 Sec 内存限制: 128 MB 提交: 4 解决: ...
- 094 Binary Tree Inorder Traversal 中序遍历二叉树
给定一个二叉树,返回其中序遍历.例如:给定二叉树 [1,null,2,3], 1 \ 2 / 3返回 [1,3,2].说明: 递归算法很简单,你可以通过迭代算法完成吗?详见 ...
- java编写二叉树以及前序遍历、中序遍历和后序遍历 .
/** * 实现二叉树的创建.前序遍历.中序遍历和后序遍历 **/ package DataStructure; /** * Copyright 2014 by Ruiqin Sun * All ri ...
随机推荐
- HTML5初学总结
基本标签的使用 <!doctype html> <!--这是HTML5的申明,大小写都可以--> <html> <head> <title> ...
- lua的table排序
lua中利用到的排序的基本上就是构造函数(table)了,为了便于和C区分开来,我俗称它为表单. 实例:(原理就是LUA集成的冒泡算法) 排序的一般姿势(对于只包含数字或者只包含字符串的简单数组) t ...
- centos 安装haproxy 1.6.3
yum install libtermcap-devel ncurses-devel libevent-devel readline-devel gcc gcc-c++ pcre pcre-devel ...
- BizTalk开发系列(十八) 使用信封拆分数据库消息
之前写了一篇的<BizTalk开发系列(十七) 信封架构(Envelop)> 是关于信封架构及其拆装原理的,都是理论性的内容.信封在BizTalk开发过程中最常用的应该是在读取SQL Se ...
- P1079 Vigenère 密码
#include <bits/stdc++.h> using namespace std; const int maxn = 1005; int main() { freopen(&quo ...
- 【java基础学习】数据库编程
数据库编程 import java.sql.*; public class JdbcDemo1{ public static void main(String[] args){ try{ //1.加载 ...
- zepto源码--核心方法9(管理包装集)--学习笔记
今天介绍的是与子元素相关的函数,children, find, contents children 从源码来看,主要是调用过滤函数filtered对遍历整个包装集返回的children进行过滤. 仔细 ...
- Linux下安装Redis3.2.4
安装: 通过wget方式直接在linux上下载Redis $ wget http://download.redis.io/releases/redis-3.2.4.tar.gz , 默认下载到路径是r ...
- linux下C++ 插件(plugin)实现技术
应用程序中使用插件技术,有利于日后的版本更新.维护(比如打补丁)和功能扩展,是一种很实用的技术.其最大的特点是更新插件时无需重新编译主程序,对于一个设计良好的应用系统而言,甚至可以做到业务功能的在线升 ...
- CentOS7 安装与配置Ant
安装前提:需安装jdk(java-1.8.0-openjdk-devel). 1.到官网下载Antt包,我下的是:apache-ant-1.9.7-bin.tar.gz 2.解压缩 tar -zxvf ...