歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> 求二叉樹第K層的節點數和二叉樹第K層的葉子節點數,遞歸和非遞歸

求二叉樹第K層的節點數和二叉樹第K層的葉子節點數,遞歸和非遞歸

日期:2017/3/1 9:34:54   编辑:Linux編程

求二叉樹第K層的節點數和二叉樹第K層的葉子節點數,遞歸方式和非遞歸方式。

1、二叉樹定義

typedef struct BTreeNodeElement_t_ {
void *data;
} BTreeNodeElement_t;


typedef struct BTreeNode_t_ {
BTreeNodeElement_t *m_pElemt;
struct BTreeNode_t_ *m_pLeft;
struct BTreeNode_t_ *m_pRight;
} BTreeNode_t;

2、求二叉樹第K層的節點數

(1)遞歸方式:

給定根節點pRoot:

如果pRoot為空,或者層數KthLevel <= 0,則為空樹或者不合要求,則返回0;

如果pRoot不為空,且此時層數KthLevel==1,則此時pRoot為第K層節點之一,則返回1;

如果pRoot不為空,且此時層數KthLevel > 1,則此時需要求pRoot左子樹(KthLevel - 1 )層節點數和pRoot右子樹(KthLevel-1)層節點數;

int GetBTreeKthLevelNodesTotal( BTreeNode_t *pRoot, int KthLevel){
if( pRoot == NULL || KthLevel <= 0 )
return 0;
if( pRoot != NULL && KthLevel == 1 )
return 1;

return (GetBTreeKthLevelNodesTotal( pRoot->m_pLeft, KthLevel-1) + GetBTreeKthLevelNodesTotal( pRoot->m_pRight, KthLevel - 1 ) );
}

(2)非遞歸方式

借助隊列實現:

int GetKthLevelNodesTotal( BTreeNode_t *pRoot, unsigned int KthLevel ){
if( pRoot == NULL )
return 0;

queue <BTreeNode_t *> que;
que.push( pRoot );
int curLevelNodesTotal = 0;
int curLevel = 0;

while( !que.empty() ){
++curLevel;//當前層數
curLevelNodesTotal = que.size();
if( curLevel == KthLevel )//如果層數等於給定層數
break;

int cntNode = 0;
while( cntNode < curLevelNodesTotal){//將下一層節點入隊
++cntNode;
pRoot = que.front();
que.pop();
if( pRoot->m_pLeft != NULL )
que.push(pRoot->m_pLeft);
if( pRoot->m_pRight != NULL )
que.push( pRoot->m_pRight);
}
}

while ( !que.empty() )
que.pop();

if( curLevel == KthLevel )
return curLevelNodesTotal;
return 0; //如果KthLevel大於樹的深度
}

3、求二叉樹第K層葉子節點數

(1)遞歸方式

給定節點pRoot:

如果pRoot為空,或者層數KthLevel <= 0, 則為空樹或者是層數非法,則返回0;

如果pRoot不為空,且此時層數KthLevel==1時,需要判斷是否為葉子節點:

如果pRoot左右子樹均為空,則pRoot為第K層葉子節點之一,則返回1;

如果pRoot左右子樹之一存在,則pRoot不是葉子節點,則返回0;

如果pRoot不為空,且此時層數KthLevel > 1,需要返回 KthLevel-1層的左子樹和右子樹結點數。

int GetBTreeKthLevelLeafNodesTotal( BTreeNode_t *pRoot, int KthLevel){
if( pRoot == NULL || KthLevel <= 0 )
return 0;

if( pRoot != NULL && KthLevel == 1 ){
if( pRoot->m_pLeft == NULL && pRoot->m_pRight == NULL )
return 1;
else
return 0;
}

return ( GetBTreeKthLevelLeafNodesTotal( pRoot->m_pLeft, KthLevel - 1) + GetBTreeKthLevelLeafNodesTotal( pRoot->m_pRight, KthLevel -1) );
}

(2)非遞歸方式

借助隊列實現

int GetKthLevelNodesTotal( BTreeNode_t *pRoot, unsigned int KthLevel ){
if( pRoot == NULL )
return 0;

queue <BTreeNode_t *> que;
que.push( pRoot );
int curLevelNodesTotal = 0;
int curLevel = 0;

while( !que.empty() ){
++curLevel;//當前層數
curLevelNodesTotal = que.size();
if( curLevel == KthLevel )//如果層數等於給定層數
break;

int cntNode = 0;
while( cntNode < curLevelNodesTotal){//將下一層節點入隊
++cntNode;
pRoot = que.front();
que.pop();
if( pRoot->m_pLeft != NULL )
que.push(pRoot->m_pLeft);
if( pRoot->m_pRight != NULL )
que.push( pRoot->m_pRight);
}
}

if( curLevel == KthLevel ){
int cntNode = 0;
int leafNodes = 0;
while( cntNode < curLevelNodesTotal ){
++cntNode;
pRoot = que.front();
que.pop();

if( pRoot->m_pLeft == NULL && pRoot->m_pRight == NULL )
leafNodes++;
}
return leafNodes; //返回葉子節點數
}

return 0; //如果KthLevel大於樹的深度
}

二叉樹的常見問題及其解決程序 http://www.linuxidc.com/Linux/2013-04/83661.htm

【遞歸】二叉樹的先序建立及遍歷 http://www.linuxidc.com/Linux/2012-12/75608.htm

在JAVA中實現的二叉樹結構 http://www.linuxidc.com/Linux/2008-12/17690.htm

【非遞歸】二叉樹的建立及遍歷 http://www.linuxidc.com/Linux/2012-12/75607.htm

二叉樹遞歸實現與二重指針 http://www.linuxidc.com/Linux/2013-07/87373.htm

二叉樹先序中序非遞歸算法 http://www.linuxidc.com/Linux/2014-06/102935.htm

輕松搞定面試中的二叉樹題目 http://www.linuxidc.com/linux/2014-07/104857.htm

Copyright © Linux教程網 All Rights Reserved