博客
关于我
数据结构 二叉树有遍历的想法
阅读量:502 次
发布时间:2019-03-07

本文共 2385 字,大约阅读时间需要 7 分钟。

#include 
#include
using namespace std;#define LEN 30typedef struct TreeNode { int data; TreeNode *LChild; TreeNode *RChild;} TreeNode;TreeNode* init(int* init_arr, int lens) { TreeNode* treeArr[LEN]; for (int i = 0; i < lens; i++) { if (init_arr[i] != 0) { treeArr[i] = (TreeNode*)malloc(sizeof(TreeNode)); treeArr[i]->data = init_arr[i]; treeArr[i]->LChild = NULL; treeArr[i]->RChild = NULL; } else { treeArr[i] = NULL; } } int len = lens / 2; for (int i = 0; i < len; i++) { if (treeArr[i] != NULL) { treeArr[i]->LChild = treeArr[2*i + 1]; treeArr[i]->RChild = treeArr[2*i + 2]; } } return treeArr[0];}void PreOrder(TreeNode* head) { if (head != NULL) { cout << head->data << " "; PreOrder(head->LChild); PreOrder(head->RChild); }}void InOrder(TreeNode* head) { if (head != NULL) { InOrder(head->LChild); cout << head->data << " "; InOrder(head->RChild); }}void PostOrder(TreeNode* head) { if (head != NULL) { PostOrder(head->LChild); PostOrder(head->RChild); cout << head->data << " "; }}void print_leaf_node(TreeNode* root) { if (root != NULL) { if (root->LChild == NULL && root->RChild == NULL) { cout << root->data << " "; } print_leaf_node(root->LChild); print_leaf_node(root->RChild); }}int Leaf_count = 0;void leaf_num1(TreeNode* root) { if (root != NULL) { leaf_num1(root->LChild); leaf_num1(root->RChild); if (root->LChild == NULL && root->RChild == NULL) { Leaf_count++; } }}int leaf_num2(TreeNode* root) { if (root == NULL) { return 0; } else if (root->LChild == NULL && root->RChild == NULL) { return 1; } else { return leaf_num2(root->LChild) + leaf_num2(root->RChild); }}int depth = 0;void Pre_tree_depth(TreeNode* head, int h) { if (head != NULL) { if (h > depth) { depth = h; } Pre_tree_depth(head->LChild, h + 1); Pre_tree_depth(head->RChild, h + 1); }}int main(int argc, char const* argv[]) { TreeNode* head = (TreeNode*)malloc(sizeof(TreeNode)); int init_arr[] = {1, 2, 3, 0, 4, 0, 5}; head = init(init_arr, 7); PreOrder(head); cout << endl;}

转载地址:http://ncccz.baihongyu.com/

你可能感兴趣的文章
python3安装scapy_Linux下安装scapy-python3
查看>>
python3实现socket通信
查看>>
Python关于numpy.transpose函数详细解析附代码
查看>>
Python3导入自定义模块的3种方式
查看>>
python3打开txt文件_使用Python 3从文本文件读取句子并添加到列表中
查看>>
python3爬虫抓取智联招聘职位信息代码
查看>>
Python3爬虫数据入数据库---把爬取到的数据存到数据库,带数据库去重功能
查看>>
python3的 pymysql把mysqldb库取代了,让python 3支持mysqldb的解决方法
查看>>
Python3笔记——IDE的选择
查看>>
python3简单使用requests 用户代理,cookie池
查看>>
Python3组播通信编程实现教程(发送者+接收者)
查看>>
Python3编写网络爬虫10-数据存储方式三-CSV文件存储
查看>>
python3读取redis返回数据带有'b'的问题
查看>>
Python3连接MySQL
查看>>
Python关于isinstance函数的解析 | 附Demo(全)
查看>>
Python3:setup.py:完成所有任务的PIP安装(Build_ext+Install)
查看>>
python4.0 gil_Python 并发编程系列--1、浅谈GIL历史及对多线程的影响
查看>>
python60行绘图程序_天底下最简单的QT画图板,就一个类,60行代码
查看>>
Python: AES加密与解密
查看>>
Python: Range() 最大尺寸;动态的还是静态的?
查看>>