博客
关于我
数据结构 二叉树有遍历的想法
阅读量: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+Requests+Unittest实战系列【二】
查看>>
python3----ASCII
查看>>
Python3-selenium自动化测试框架,结合HTMLTestRunner测试报告!
查看>>
Python3-如何自我更新作为服务运行的应用程序
查看>>
Python3.1-网格模拟概念性问题
查看>>
Python3.12 解决conda环境libstdc++版本过低问题
查看>>
python3.2批量查询IP地址区域
查看>>
Python3.5中协程和未来/任务之间的区别?
查看>>
python3.5实现socket通讯(TCP)
查看>>
python3.6 ubuntu部署nginx、 uwsgi、 django
查看>>
Python3.6.3中,functools似乎不能用
查看>>
python3.6.5+cuda9+cudnn7.1+win10+tensorflow-gpu1.9.0下载配置
查看>>
python3.6使用pickle序列化class
查看>>
python3.6入门到高阶(全栈) day027 网络通信协议 osi七层模型
查看>>
Python3.6属性错误:模块‘;异步没有属性‘;运行‘;
查看>>
python3.6版本中的python-weka-wrapper安装失败,错误代码为%1
查看>>
Python3.7安装cv2踩过的坑
查看>>
python写txt文件
查看>>
python关于sorted排序函数详细解析 附实战代码(全)
查看>>
python3.8调用m文件,调用matlab
查看>>