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 ...
随机推荐
- JAVA 线程同步异步简单实例
package test; public class testThread { public static void main(String[] args) { Example example = n ...
- js 原生 ajax 异步上传图片
<script type="text/javascript"> function upload() { var file1 = document.getElementB ...
- UIBezierPath 的使用
使用UIBezierPath类可以创建基于矢量的路径,这个类在UIKit中.此类是Core Graphics框架关于path的一个封装.使用此类可以定义简单的形状,如椭圆或者矩形,或者有多个直线和曲线 ...
- redis 安装(centos 6.4)
我使用6.4系统,mark一下,其他版本应该也一样. wget wget http://download.redis.io/releases/redis-3.0.6.tar.gz make make ...
- STL_关联容器 VS C++ hashmap
红黑树和哈希表区别: http://m.blog.csdn.net/article/details?id=52133283 关于STL中关联容器的几个问题: (1)为何map和set的插入删除效率比用 ...
- Algorithm | Tree traversal
There are three types of depth-first traversal: pre-order,in-order, and post-order. For a binary tre ...
- JQuery-文档处理&选择器
<body> <div id="textDiv">...</div> <p>第一段..</p> <b>第二段 ...
- 8 ways rich people view the world differently than the average person
Self-made millionaire Steve Siebold spent 26 years interviewing some of the wealthiest people in the ...
- java基础算法-快速排序
玩博客园很多年,第一次写点什么,就从基础开始吧.最近去面试,发现自己算法忘光了,赶紧复习下.以下代码自带测试类,复制进eclipse中右键 run as -->java application ...
- iOS:命令行方式使用OSChina托管私有代码
一.介绍 在项目开发中,使用版本控制工具是必不可少的开发工具,它可以帮助我们程序员写完代码后及时提交备份,防止因个人操作导致代码被误删除了或者丢失了,安全可靠.同时,使用版本控制器工具也可以很方便的进 ...