c++ 递归版本遍历二叉树
来源:互联网 发布:深圳旅游知乎 编辑:程序博客网 时间:2024/05/20 23:38
#include <iostream>
struct BinTreeNode {
int value ;
BinTreeNode * leftChild, *rightChild;
BinTreeNode(int data) : value(data), leftChild(nullptr), rightChild(nullptr) {};
};
void preOrderRecur(const BinTreeNode * root);
void midOrderRecur(const BinTreeNode * root);
void lastOrderRecur(const BinTreeNode * root);
int main()
{
BinTreeNode root(5);
BinTreeNode n1(9);
BinTreeNode n2(8);
BinTreeNode n3(7);
BinTreeNode n4(6);
BinTreeNode n5(5);
BinTreeNode n6(4);
root.leftChild = &n1;
root.rightChild = &n4;
n1.leftChild = &n2;
n1.rightChild = &n3;
n3.rightChild = &n6;
n4.leftChild = &n5;
std::cout << "preOrderRecur: ";
preOrderRecur(&root);
std::cout << std::endl;
std::cout << "midOrderRecur: ";
midOrderRecur(&root);
std::cout << std::endl;
std::cout << "lastOrderRecur: ";
lastOrderRecur(&root);
std::cout << std::endl;
system("pause");
return 0;
}
void preOrderRecur(const BinTreeNode * root)
{
if (root == nullptr)
{
return;
}
std::cout << root->value << " ";
preOrderRecur(root->leftChild);
preOrderRecur(root->rightChild);
}
void midOrderRecur(const BinTreeNode * root)
{
if (root == nullptr)
{
return;
}
preOrderRecur(root->leftChild);
std::cout << root->value << " ";
preOrderRecur(root->rightChild);
}
void lastOrderRecur(const BinTreeNode * root)
{
if (root == nullptr)
{
return;
}
preOrderRecur(root->leftChild);
preOrderRecur(root->rightChild);
std::cout << root->value << " ";
}
struct BinTreeNode {
int value ;
BinTreeNode * leftChild, *rightChild;
BinTreeNode(int data) : value(data), leftChild(nullptr), rightChild(nullptr) {};
};
void preOrderRecur(const BinTreeNode * root);
void midOrderRecur(const BinTreeNode * root);
void lastOrderRecur(const BinTreeNode * root);
int main()
{
BinTreeNode root(5);
BinTreeNode n1(9);
BinTreeNode n2(8);
BinTreeNode n3(7);
BinTreeNode n4(6);
BinTreeNode n5(5);
BinTreeNode n6(4);
root.leftChild = &n1;
root.rightChild = &n4;
n1.leftChild = &n2;
n1.rightChild = &n3;
n3.rightChild = &n6;
n4.leftChild = &n5;
std::cout << "preOrderRecur: ";
preOrderRecur(&root);
std::cout << std::endl;
std::cout << "midOrderRecur: ";
midOrderRecur(&root);
std::cout << std::endl;
std::cout << "lastOrderRecur: ";
lastOrderRecur(&root);
std::cout << std::endl;
system("pause");
return 0;
}
void preOrderRecur(const BinTreeNode * root)
{
if (root == nullptr)
{
return;
}
std::cout << root->value << " ";
preOrderRecur(root->leftChild);
preOrderRecur(root->rightChild);
}
void midOrderRecur(const BinTreeNode * root)
{
if (root == nullptr)
{
return;
}
preOrderRecur(root->leftChild);
std::cout << root->value << " ";
preOrderRecur(root->rightChild);
}
void lastOrderRecur(const BinTreeNode * root)
{
if (root == nullptr)
{
return;
}
preOrderRecur(root->leftChild);
preOrderRecur(root->rightChild);
std::cout << root->value << " ";
}
阅读全文
0 0
- 二叉树非递归遍历(C++版本)
- c++ 递归版本遍历二叉树
- 二叉树遍历的递归与非递归版本
- 二叉树的前序中序后序遍历 - 递归&非递归版本
- 【C++】非递归遍历二叉树
- 二叉树非递归遍历(C++)
- 二叉树的建立,前中后序遍历的递归版本和非递归版本,层序遍历
- 先中后序递归非递归遍历二叉树(c#)
- 二叉树的递归与非递归遍历源码(C++)
- 二叉树的递归,非递归遍历(C++)
- C语言二叉树的遍历,递归和非递归
- 【C++】二叉树遍历--递归与非递归实现
- 二叉树的创建与遍历(递归版本)
- 二叉树 前中后序遍历非递归版本 C++实现
- 二叉树遍历的非递归实现 java版本
- 二叉树非递归遍历实现-java版本
- 二叉树递归遍历
- 二叉树遍历(递归)
- 找出耗CPU的java线程脚本-优化版
- hibernate配置(略)
- Python range和xrange
- Linux中使用top+jstack分析线程状态
- ssm框架搭建
- c++ 递归版本遍历二叉树
- CS231n Convolutional Neural Networks for Visual Recognition
- HighCharts(2)坐标轴5部分
- Javascript and DOM
- 分代垃圾收集器
- hdu6140 bitset
- js各种排序汇总
- ubuntu 12.04 安装编译L2.6.35_1.1.0_130130 基于 i.mx28
- sudo: javac:找不到命令