NOJ1018-深度遍历二叉树
题目要求很简单,前中后序遍历一棵二叉树。坑爹的是这道题的输入数据和测试数据压根不一样,找了好久原因,去讨论区看见有别人发的测试样例,修改了一下就AC了
测试样例是这个:DEH##FJ##G#CK###A#B##
#include <cstdio>
typedef char TElemType;
typedef struct node {
TElemType data;
struct node *left_child;
struct node *right_child;
} BTNode, *BinTree;
//TElemType ch[] = { 'A', 'B', '#', 'D', '#', '#', 'C', 'E', '#', '#', 'F', '#', '#' };
//DEH##FJ##G#CK###A#B##
TElemType ch[] = { 'D', 'E', 'H', '#', '#', 'F', 'J', '#', '#', 'G', '#', 'C', 'K', '#', '#', '#', 'A', '#', 'B', '#', '#' };
int count = ;
void Create( BTNode*& t ) {
char c;
c = ch[count++];
if( c =='#' )
t = NULL;
else {
t = new BTNode;
t->data = c;
Create( t->left_child );
Create( t->right_child );
}
}
void PreOrder( BTNode* t ) {
if( t != NULL ) {
printf( " %c", t->data );
PreOrder( t->left_child );
PreOrder( t->right_child );
}
}
void InOrder( BTNode *t ) {
if( t != NULL ) {
InOrder( t->left_child );
printf( " %c", t->data );
InOrder( t->right_child );
}
}
void PostOrder( BTNode *t ) {
if( t != NULL ) {
PostOrder( t->left_child );
PostOrder( t->right_child );
printf( " %c", t->data );
}
}
int main() {
BTNode T;
BinTree root = &T;
Create( root );
printf( "PreOrder:" );
PreOrder( root );
printf( "\n" );
printf( "InOrder:" );
InOrder( root );
printf( "\n" );
printf( "PostOrder:" );
PostOrder( root );
return ;
}
NOJ1018-深度遍历二叉树的更多相关文章
- 递归/非递归----python深度遍历二叉树(前序遍历,中序遍历,后序遍历)
递归代码:递归实现很简单 '二叉树结点类' class TreeNode: def __init__(self, x): self.val = x self.left = None self.righ ...
- 【面经】用递归方法对二叉树进行层次遍历 && 二叉树深度
void PrintNodeAtLevel(BiTree T,int level) { // 空树或层级不合理 ) return; == level) { cout << T->da ...
- Java遍历二叉树深度宽度
节点数据结构 class TreeNode { TreeNode left = null; TreeNode right = null; } 最大深度,基本思路是:使用递归,分别求出左子树的深度.右子 ...
- YTU 2345: 后序遍历二叉树
原文链接:https://www.dreamwings.cn/ytu2345/2611.html 2345: 后序遍历二叉树 时间限制: 1 Sec 内存限制: 128 MB 提交: 3 解决: ...
- YTU 2346: 中序遍历二叉树
原文链接:https://www.dreamwings.cn/ytu2346/2606.html 2346: 中序遍历二叉树 时间限制: 1 Sec 内存限制: 128 MB 提交: 12 解决: ...
- YTU 2344: 先序遍历二叉树
原文链接:https://www.dreamwings.cn/ytu2344/2603.html 2344: 先序遍历二叉树 时间限制: 1 Sec 内存限制: 128 MB 提交: 4 解决: ...
- Java递归方法遍历二叉树的代码
将内容过程中经常用的内容做个记录,如下内容内容是关于Java递归方法遍历二叉树的内容. package com.wzs; public class TestBinaryTree { public st ...
- 【遍历二叉树】10判断二叉树是否平衡【Balanced Binary Tree】
平衡的二叉树的定义都是递归的定义,所以,用递归来解决问题,还是挺容易的额. 本质上是递归的遍历二叉树. ++++++++++++++++++++++++++++++++++++++++++++++++ ...
- 深度遍历DFS---树
一.二叉树的深度 题目: 给定一个二叉树,找出其最大深度. 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数. 说明: 叶子节点是指没有子节点的节点. 示例: 给定二叉树 [3,9,20,nul ...
- JAVA递归、非递归遍历二叉树
前序遍历:1.访问根节点 2.前序遍历左子树 3.前序遍历右子树 中序遍历:1.中序遍历左子树 2.访问根节点 3.中序遍历右子树 后序遍历:1.后序遍历左子树 2.后序遍历右子树 3.访问根节点-- ...
随机推荐
- Linux操作系统下三种配置环境变量的方法——转载
来源:赛迪网 作者:millio 现在使用linux的朋友越来越多了,在linux下做开发首先就是需要配置环境变量,下面以配置java环境变量为例介绍三种配置环境变量的方法. 1.修改/e ...
- 在java 中,数组与 List<T> 类型的相互转换
在java中,数组与List<T> 之前进行互相转换,转换方法可总结为以下几种: 一. 将 数组转换成List<T> 1. 使用 Collections 的addAll 方法 ...
- 学习练习 java 输入输出流练习2
编写IoDemo.java的Java应用程序,程序完成的功能是:首先读取text.txt文件内容,再通过键盘输入文件的名称为iodemo.txt,把text.txt的内容存入iodemo.txt p ...
- ajax 跳入error的一些原因
先放一个标准的jquery的ajax代码: $.ajax({ type: 'POST', url: 'getSecondClassification', data: {"sort2" ...
- docker学习(一)
atomic使用有点费劲,我改为centos7来做为学习环境. 1 安装 epel源就自带,目前版本是1.10.3 yum -y install docker docker version Clien ...
- 【Python】django权限管理
参考:http://www.cnblogs.com/esperyong/ 参考:https://docs.djangoproject.com/en/1.8/topics/auth/default/#t ...
- angular.extend(dst, src)对象拓展
angular.extend(dst, src) 作用:对象的拓展 参数: dst:拓展的对象 src:源对象 返回值:拓展的对象 var dst = {name: 'xxx', country: ...
- git gc
git gc 有时候当你运行一些git命令(比如git pull)的时候,会有如下提示: 1 2 Auto packing the repository for optimum performance ...
- ORA-12518 TNS:监听程序无法分发客户机连接 解决办法
查询的脚本: select count(*) from v$process; --取得数据库目前的进程数. select value from v$parameter where name = 'pr ...
- poj2503 哈希
这题目主要是难在字符串处理这块. #include <stdio.h> #include <string.h> #include <stdlib.h> #defin ...