問題描述;算法
若是咱們把二叉樹當作一個圖,父子節點之間的連線當作是雙向的,咱們姑且定義"距離"爲兩節點之間邊的個數。寫一個程序求一棵二叉樹中相距最遠的兩個節點之間的距離。spa
算法很容易想獲得:code
/* Name: main.c Author: suzhou Date: 2014.02.18 Num. 2 */ #include "btree.h" int main() { BTree mytree = NULL; int choice; while (1) { printf("\n請選擇您想要進行的操做:\n"); printf(" [ 1 ] 先序次序創建二叉樹\n"); printf(" [ 2 ] 先序打印二叉樹節點\n"); printf(" [ 3 ] 求樹的深度\n"); printf(" [ 4 ] 求樹中相距最遠的兩個節點的距離\n"); printf(" [ 0 ] 退出\n"); scanf("%d", &choice); switch(choice) { case 0: printf("%s\n", "謝謝使用"); exit(0); case 1: printf("%s", "請以先序遍歷次序輸入各個節點的數值:\n"); createBTree(&mytree); continue; case 2: preOrderVisit(mytree); continue; case 3: printf("樹的深度爲: %d\n", depth(mytree)); continue; case 4: printf("最大距離爲: %d\n", maxDistance(mytree) ); continue; default: printf("%s\n", "請輸入正確的選擇"); continue; } } printf("Congratulations! It works!\n"); return 0; }
/* Name: btree.h Author: suzhou Date: 2014.02.18 Num. 2 */ #ifndef BTREE #define BTREE #include "stdio.h" #include "stdlib.h" typedef struct BTNode { int val; struct BTNode* pLeft; struct BTNode* pRight; }BTNode, *BTree; /* 創建二叉樹 */ void createBTree ( BTree* tree ); /* 先序遍歷二叉樹 */ void preOrderVisit ( BTree tree ); /* 按樹狀打印節點 */ void printTree ( BTree tree, int depth ); /* * 求樹的深度 */ int depth( BTree tree ); /* * 求樹的兩個節點的最遠距離 */ int maxDistance( BTree tree ); #endif
/* Name: btree.c Author: suzhou Date: 2014.02.18 Num. 2 */ #include "btree.h" /* 創建二叉樹 */ void createBTree ( BTree* tree ) { int val; scanf("%d", &val); if ( val == 0 ) { *tree = NULL; } else { *tree = (BTNode*) malloc (sizeof(BTNode)); (*tree)->val = val; createBTree(&((*tree)->pLeft)); createBTree(&((*tree)->pRight)); } } /* 先序遍歷二叉樹 */ void preOrderVisit ( BTree tree ) { if ( tree != NULL ) { printf("%d\n", tree->val); preOrderVisit(tree->pLeft); preOrderVisit(tree->pRight); } } /* 按樹狀打印節點 */ void printTree ( BTree tree, int depth ) { int i; if ( tree == NULL ) return; for ( i=depth; i>=0; i-- ) printf("%c", ' '); printf("%d\n" , tree->val); printTree(tree->pLeft, depth+1); printTree(tree->pRight, depth+1); } /* * 求樹的深度 */ int depth( BTree tree ) { if (tree == NULL) return 0; else return 1 + (depth(tree->pLeft) >= depth(tree->pRight) ? depth(tree->pLeft) : depth(tree->pRight)); } /* * 求樹的兩個節點的最遠距離 */ int maxDistance( BTree tree ) { if (tree->pLeft == NULL || tree->pRight == NULL) return depth(tree); else return depth(tree->pLeft) + depth(tree->pRight); }