这里主要是三种遍历,先序(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. Beautifulsoup关于find的测试

    from bs4 import BeautifulSoup import requests url='https://book.douban.com/subject_search?search_tex ...

  2. django-debug-toolbar和Django 日志配置

    django-debug-toolbar介绍 django-debug-toolbar 是一组可配置的面板,可显示有关当前请求/响应的各种调试信息,并在单击时显示有关面板内容的更多详细信息. gith ...

  3. 原生CSS设置网站主题色—CSS变量赋值

    定义CSS变量 在css文件顶部定义css变量,注意必须以--开头,使用:root包括这几个变量 :root { --main-bg-color: #ff7675; --color1: #fbfee9 ...

  4. CSharp遗传算法求解背包问题

    断断续续写了四天,感觉背包问题是最适合了解遗传算法的问题模型 using System; using System.Collections.Generic; using System.Linq; us ...

  5. 2. Spring 的 HelloWorld

    初学Spring,就先来写一个 Spring 的 HelloWorld 吧 1. 首先,新建一个 java Project(因为暂时不需要网页,所以就不用创建 web 项目了) 2. 导入 Sprin ...

  6. 前端利器Emmet

    前端利器Emmet Emmet可以通过缩写生成代码片段,可以提升前端开发的效率.使用方法就是输入HTML或者CSS的缩写,然后按tab键自动生成.原文在这里 后代 > <!-- nav&g ...

  7. MySQL创建用户的三种方法 (并授权)转

    前言:MySQL创建用户的方法分成三种:INSERT USER表的方法.CREATE USER的方法.GRANT的方法. 一.账号名称的构成方式 账号的组成方式:用户名+主机(所以可以出现重复的用户名 ...

  8. Docker命令使用详解(转)

    如果各位看官熟悉 Git 和 GitHub ,可与 Docker 做个类比,可更加容易理解 Docker 和 Docker Hub 及两者关系. 1. docker version 显示 Docker ...

  9. java获得上下周及本周日期

    public static SimpleDateFormat getFormat(String format) { return new SimpleDateFormat(format); } /** ...

  10. Python3 与 NetCore 基础语法对比(List、Tuple、Dict、Set专栏)

    Jupyter最新版:https://www.cnblogs.com/dotnetcrazy/p/9155310.html 在线演示:http://nbviewer.jupyter.org/githu ...