博客
关于我
数据结构 二叉树有遍历的想法
阅读量: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/

你可能感兴趣的文章
Objective-C实现lstm prediction预测算法(附完整源码)
查看>>
Objective-C实现lucas数列算法(附完整源码)
查看>>
Objective-C实现Luhn (Mod 10)Algorithm算法(附完整源码)
查看>>
Objective-C实现LZW编码(附完整源码)
查看>>
Objective-C实现MAC桌面暗水印(附完整源码)
查看>>
Objective-C实现mandelbrot曼德勃罗特集算法(附完整源码)
查看>>
Objective-C实现markov chain马尔可夫链算法(附完整源码)
查看>>
Objective-C实现MATLAB中Filter函数功能(附完整源码)
查看>>
Objective-C实现matrix chainorder矩阵链顺序算法(附完整源码)
查看>>
Objective-C实现matrix exponentiation矩阵求幂算法(附完整源码)
查看>>
Objective-C实现MatrixMultiplication矩阵乘法算法 (附完整源码)
查看>>
Objective-C实现max non adjacent sum最大非相邻和算法(附完整源码)
查看>>
Objective-C实现max subarray sum最大子数组和算法(附完整源码)
查看>>
Objective-C实现max sum sliding window最大和滑动窗口算法(附完整源码)
查看>>
Objective-C实现MaxHeap最大堆算法(附完整源码)
查看>>
Objective-C实现MaximumSubarray最大子阵列(Brute Force蛮力解决方案)算法(附完整源码)
查看>>
Objective-C实现MaximumSubarray最大子阵列(动态规划解决方案)算法(附完整源码)
查看>>
Objective-C实现maxpooling计算(附完整源码)
查看>>
Objective-C实现max_difference_pair最大差异对算法(附完整源码)
查看>>
Objective-C实现max_heap最大堆算法(附完整源码)
查看>>