树的遍历-Preorde Traversal,Inorder Traversal,Postoder Traversal
上代码(创建树,先序,中序,后序)
#include<bits/stdc++.h>
using namespace std;
typedef struct TreeNode *BinTree;
struct TreeNode
{BinTree left,right;int value;
};
BinTree CreateTree()
{BinTree T;int value;cin>>value;if(value==0) //如果输入0,则创建树停止{T=NULL;}else{T=(BinTree)malloc(sizeof(TreeNode));T->value=value;T->left=CreateTree();T->right=CreateTree();}return T;
}
void Preorder(BinTree T)//Preorder Traversal,root first,left-child second,right-child third.
{if(T==NULL){return;}cout<<T->value<<endl;Preorder(T->left);Preorder(T->right);return;
}
void Inorder(BinTree T)//Inorder Traversa,left-child first,root second,right-child third.
{if(T==NULL){return;}Inorder(T->left);cout<<T->value<<endl;Inorder(T->right);return ;
}
void Postorder(BinTree T)//Postorder Traversal,left-child first,right-child second,root third.
{if(T==NULL){return ;}Postorder(T->left);Postorder(T->right);cout<<T->value<<endl;return ;
}
int main()
{BinTree root;root=CreateTree();Preorder(root);Inorder(root);Postorder(root);return 0;
}
代码暂不做解释,留个人参考使用.
测试数据:
input:
1 2 4 0 0 5 0 0 3 0 0
output:
1 2 4 5 3
4 2 5 1 3
4 5 2 3 1
(将换行更改一下)
树的遍历-Preorde Traversal,Inorder Traversal,Postoder Traversal相关推荐
- 数据结构思维 第六章 树的遍历
第六章 树的遍历 原文:Chapter 6 Tree traversal 译者:飞龙 协议:CC BY-NC-SA 4.0 自豪地采用谷歌翻译 本章将介绍一个 Web 搜索引擎,我们将在本书其余部分开 ...
- tree traversal (树的遍历) - inorder traversal (中序遍历)
tree traversal (树的遍历) - inorder traversal (中序遍历) 1. tree traversal - 树的遍历 二叉树的遍历 (traversing binary ...
- tree traversal (树的遍历) - postorder traversal (后序遍历)
tree traversal (树的遍历) - postorder traversal (后序遍历) 1. tree traversal - 树的遍历 二叉树的遍历 (traversing binar ...
- tree traversal (树的遍历) - preorder traversal (前序遍历)
tree traversal (树的遍历) - preorder traversal (前序遍历) 1. tree traversal - 树的遍历 二叉树的遍历 (traversing binary ...
- tree traversal (树的遍历) - 层序遍历 (level order traversal) - 二叉树的层序遍历
tree traversal (树的遍历) - 层序遍历 (level order traversal) - 二叉树的层序遍历 1. tree traversal (树的遍历) 1.1 深度优先搜索 ...
- Construct Binary Tree from Inorder and Postorder Traversal
题目 Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume ...
- LeetCode - 106 - Construct Binary Tree from Inorder and Postorder Traversal
Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume tha ...
- 【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal
Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...
- leetcode 刷题之路 64 Construct Binary Tree from Inorder and Postorder Traversal
Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume tha ...
最新文章
- vbscript错误代码及对应解释大全[z]
- 斯坦福大学深度学习与自然语言处理第一讲引言
- PE 学习之路 —— 区块表
- 移动spa商城优化记(一)---首屏优化篇
- 加速ASP.NET Core WEB API应用程序——第2部分
- 李开复对谈硅谷传奇:杨致远敦促AI交产品,马尔科夫说无人车3年没戏
- js 小数自动补0_JS自定义保留小数,并支持补零(四舍五入)
- Contains Duplicate 包含重复值
- 易语言代码转php,易语言代码转PHP代码有没大佬
- 数值计算之 梯度向量和梯度矩阵,雅可比矩阵,海森矩阵
- 整理wind商誉数据2016-2019
- 通过日期的相减计算年龄
- 一键式免费借还的智能充电宝租赁
- 电脑卡顿?性能不足?一套连招榨干你的电脑!
- 永琳的竹林迷径(path)
- 恒生电子股份有限公司--软件测试--《社招、校招jd、校招行程,招聘动态》整理
- 少儿编程课程体系需求
- authorize(权限验证)
- 【带你看看开源圈的新趋势】GITHUB OCTOVERSE 2022 详细解读
- 我的Java学习感悟
热门文章
- SAP 那点事BW HANA
- VC6下使用WebLink控件
- Docker中安装Jenkins实时发布.net core 项目(一)
- 注册机patch起什么作用_电机滑环起什么作用?
- $.each()、$.map()区别浅谈
- php查询数据方法,php查询数据库的方法
- sqlserver note
- 1004 成绩排名 (20分)
- JQuery 中选择多选择框,和单选框,实现获取相应选择的值
- 13 python初学(函数)