生活随笔
收集整理的這篇文章主要介紹了
求二叉树的高度
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
?這個程序在二叉樹層次遍歷的遞歸實現中曾用到過,程序如下:
[cpp]?view plaincopy
#include<iostream>?? #include<stack>?? #define?N?7?? using?namespace?std;?? ?? typedef?struct?node?? {?? ????struct?node?*leftChild;?? ????struct?node?*rightChild;?? ????int?data;?? }BiTreeNode,?*BiTree;?? ?? ?? BiTreeNode?*createNode(int?i)?? {?? ????BiTreeNode?*?q?=?new?BiTreeNode;?? ????q->leftChild?=?NULL;?? ????q->rightChild?=?NULL;?? ????q->data?=?i;?? ?? ????return?q;?? }?? ?? BiTree?createBiTree()?? {?? ????BiTreeNode?*p[N];?? ????int?i;?? ????for(i?=?0;?i?<?N;?i++)?? ????????p[i]?=?createNode(i?+?1);?? ?? ?????? ????for(i?=?0;?i?<?N/2;?i++)?? ????{?? ????????p[i]->leftChild?=?p[i?*?2?+?1];?? ????????p[i]->rightChild?=?p[i?*?2?+?2];?? ????}?? ?? ????return?p[0];?? }?? ?? int?max(int?x,?int?y)?? {?? ????return?x?>?y???x?:?y;?? }?? ?? int?getDepth(BiTree?T)?? {?? ????if(NULL?==?T)?? ????????return?0;?? ????return?1?+?max(getDepth(T->leftChild),?getDepth(T->rightChild));?? }?? ?? int?main()?? {?? ????BiTree?T?=?createBiTree();?? ????cout?<<?getDepth(T)?<<?endl;?? ?????? ????return?0;?? } ?
總結
以上是生活随笔為你收集整理的求二叉树的高度的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。