二叉树的构建和遍历
http://blog.csdn.net/bitboss/article/details/53310234
#include <stdio.h> #include <malloc.h> typedef struct Node { struct Node* left; struct Node* right; int data; } node; int index=0; //前序创建二叉树 node* create_tree(int array[], int size, int data) { node* root = NULL; if(index<size && array[index]!='#') { root = (node*)malloc(sizeof(node)); ++index; root->left = create_tree(array, size, data); ++index; root->right = create_tree(array, size, data); } return root; } int max_distance(node *root, int maxlenth) { if(root == NULL) return 0; int LeftDepth = max_distance(root->left, maxlenth); int RightDepth = max_distance(root->right, maxlenth); int temp = LeftDepth + RightDepth; if(temp > maxlenth) maxlenth = temp; return LeftDepth>RightDepth?LeftDepth+1:RightDepth+1; } int main() { int array[15] = {1,2,1,3,'#','#',4,5,'#',6,'#',7,'#','#',8}; node *root = create_tree(array, 15, '#'); int maxlenth=0; int max = max_distance(root, maxlenth); printf("%d\n", max); return 0; }优质内容筛选与推荐>>