这里主要是三种遍历,先序(preorder,NLR),中序(Inorder,LNR),后序(Postorder,LRN)

N:node,L:left,R:right

基本排序:先序(NLR,节点,左,右),中序(LNR,左,节点,右),后序(LRN,左,右,节点)

要点:在每一种排序里,必须遵守基本排序。看图:

为了更加直观的了解,看下面的c语言实现的代码,参考了:https://www.geeksforgeeks.org/tree-traversals-inorder-preorder-and-postorder/

#include<cstdio>
#include<cstdlib>
using namespace std;
struct node{
int data;
struct node* left;
struct node* right;
};
struct node* newNode(int data){
struct node* node = (struct node*)malloc(sizeof(struct node));
node->data=data;
node->left=NULL;
node->right=NULL;
return node;
}
void printPostorder(struct node* node){
if(node == NULL)
return;
printPostorder(node->left);
printPostorder(node->right);
printf("%d ",node->data);
}
void printInorder(struct node* node){
if(node==NULL){
return;
}
printInorder(node->left);
printf("%d ",node->data);
printInorder(node->right);
}
void printPreorder(struct node* node){
if(node==NULL){
return;
}
printf("%d ",node->data);
printPreorder(node->left);
printPreorder(node->right);
}
int main(){
struct node *root=newNode();
root->left=newNode();
root->right=newNode();
root->left->left=newNode();
root->left->right=newNode();
root->right->left=newNode();
root->right->right=newNode();
root->left->left->left=newNode();
root->left->left->right=newNode();
root->left->right->left=newNode();
root->left->right->right=newNode();
root->right->left->left=newNode();
root->right->left->right=newNode();
root->right->right->left=newNode();
root->right->right->right=newNode();
printf("\nPreorder raversal of binary tree is \n");
printPreorder(root);
printf("\nInorder raversal of binary tree is \n");
printInorder(root);
printf("\nPostorder raversal of binary tree is \n");
printPostorder(root);
return ;
}

输出:

Preorder raversal of binary tree is

Inorder raversal of binary tree is

Postorder raversal of binary tree is
              

写一个中序输出的图解:

二叉树遍历-c实现的更多相关文章

  1. C++ 二叉树遍历实现

    原文:http://blog.csdn.net/nuaazdh/article/details/7032226 //二叉树遍历 //作者:nuaazdh //时间:2011年12月1日 #includ ...

  2. python实现二叉树遍历算法

    说起二叉树的遍历,大学里讲的是递归算法,大多数人首先想到也是递归算法.但作为一个有理想有追求的程序员.也应该学学非递归算法实现二叉树遍历.二叉树的非递归算法需要用到辅助栈,算法着实巧妙,令人脑洞大开. ...

  3. 【二叉树遍历模版】前序遍历&&中序遍历&&后序遍历&&层次遍历&&Root->Right->Left遍历

    [二叉树遍历模版]前序遍历     1.递归实现 test.cpp: 12345678910111213141516171819202122232425262728293031323334353637 ...

  4. hdu 4605 线段树与二叉树遍历

    思路: 首先将所有的查询有一个vector保存起来.我们从1号点开始dfs这颗二叉树,用线段树记录到当前节点时,走左节点的有多少比要查询该节点的X值小的,有多少大的, 同样要记录走右节点的有多少比X小 ...

  5. poj2255 (二叉树遍历)

    poj2255 二叉树遍历 Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu   Descripti ...

  6. D - 二叉树遍历(推荐)

    二叉树遍历问题 Description   Tree Recovery Little Valentine liked playing with binary trees very much. Her ...

  7. 二叉树遍历 C#

    二叉树遍历 C# 什么是二叉树 二叉树是每个节点最多有两个子树的树结构 (1)完全二叉树——若设二叉树的高度为h,除第 h 层外,其它各层 (1-h-1) 的结点数都达到最大个数,第h层有叶子结点,并 ...

  8. 二叉树——遍历篇(递归/非递归,C++)

    二叉树--遍历篇 二叉树很多算法题都与其遍历相关,笔者经过大量学习.思考,整理总结写下二叉树的遍历篇,涵盖递归和非递归实现. 1.二叉树数据结构及访问函数 #include <stdio.h&g ...

  9. 二叉树遍历(flist)(二叉树,已知中序层序,求先序)

    问题 C: 二叉树遍历(flist) 时间限制: 1 Sec  内存限制: 128 MB提交: 76  解决: 53[提交][状态][讨论版][命题人:quanxing][Edit] [TestDat ...

  10. 二叉树遍历(flist)(已知中序和按层遍历,求先序 )

    问题 F: 二叉树遍历(flist) 时间限制: 1 Sec  内存限制: 128 MB提交: 11  解决: 9[提交][状态][讨论版][命题人:quanxing][Edit] [TestData ...

随机推荐

  1. netty简单NIO模型

    首先是使用java原生nio类库编写的例子,开发一套nio框架不简单,所以选择了netty,该例完成后,是netty举例. package com.smkj.netty; public class T ...

  2. 实现DataGridView控件中CheckBox列的使用

    最近做WindowsForms程序,使用DataGridView控件时,加了一列做选择用,发现CheckBox不能选中.搜索后,要实现DataGridView的CellContentClick事件,将 ...

  3. mysql高级、索引

    一.mysql高级 1.视图 # 引子 select * from emp left join dep on emp.dep_id = dep.id union select * from emp r ...

  4. LeetCode(119. 杨辉三角 II)

    问题描述 给定一个非负索引 k,其中 k ≤ 33,返回杨辉三角的第 k 行. 在杨辉三角中,每个数是它左上方和右上方的数的和. 示例: 输入: 3 输出: [1,3,3,1] 进阶: 你可以优化你的 ...

  5. VS Code打造一个完美的Springboot开发环境

    对于使用Springboot环境开发java应用,首选IDE还是IntelliJ IDEA(2018),当前版本已经很流畅了,现在开发用的电脑配置基本都能够很6的跑起来,IDEA用起来真心爽啊,比Ec ...

  6. C#中#region和#endregion的用法

    一.用法说明 #region 说明 Program1 #endregion 二.作用 (1)注释其中间的代码段 (2)折叠中间的代码块(折叠后的说明文字为#region后面的说明)

  7. Docker安装管理界面portainer

    在Ubuntu或者Debian已经部署完毕Docker 拉取镜像文件: sudo docker pull docker.io/portainer/portainer Using default tag ...

  8. .net 4.0 中的特性总结(一):dynamic

    在新版本的C#中,dynamic关键词是一个很重要的新特性,现在你可以创建动态对象并在运行时再决定它的类型.而且.net 4.0为CLR加入了一组为动态语言服务的运行时环境,称为DLR(Dynamic ...

  9. UVALive - 4885 Task 差分约束

    Task 题目连接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page ...

  10. 【二维树状数组】计数问题 @JSOI2009/upcexam5911

    时间限制: 1 Sec 内存限制: 128 MB 题目描述 一个n*m的方格,初始时每个格子有一个整数权值.接下来每次有2种操作: 改变一个格子的权值: 求一个子矩阵中某种特定权值出现的个数. 输入 ...