您的位置:首页 > 其它

根据一个已排序数组构造一棵二叉树,要求树的高度最少

2012-07-26 00:36 288 查看
Node* creatBTree(int *pArray, int startPos, int endPos)
{
if (endPos < startPos)
{
return NULL;
}

if (startPos == endPos)
{
Node* pNode = new Node(pArray[startPos]);
pNode->leftChild  = NULL;
pNode->rightChild = NULL;
return pNode;
}

int middle = (startPos+endPos)/2;
Node* pNode = new Node(pArray[middle]);
pNode->leftChild  = createBTree(pArray, startPos, middle-1);
pNode->rightChild = createBTree(pArray, middle+1, endPos),

return pNode;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐