对于成长,年龄不是记号,责任才是标志,长大就是一种勇气和担当。 成长是生命的延续、责任的延续!–林海音
二叉树的面试题(C版本)
typedef struct TreeNode Node
;
void _PreOrder(Node
* pRoot
, int* pRes
, int* index
){
if (pRoot
){
pRes
[*index
] = pRoot
->val
;
(*index
)++;
_PreOrder(pRoot
->left
, pRes
, index
);
_PreOrder(pRoot
->right
, pRes
, index
);
}
}
int GetTreeSize(Node
* pRoot
){
if (NULL == pRoot
){
return 0;
}
return GetTreeSize(pRoot
->left
) + GetTreeSize(pRoot
->right
) + 1;
}
int* preorderTraversal(struct TreeNode
* root
, int* returnSize
){
*returnSize
= GetTreeSize(root
);
int* pRes
= (int*)malloc(sizeof(int)*(*returnSize
));
if (NULL == pRes
){
assert(0);
return NULL;
}
int index
= 0;
_PreOrder(root
,pRes
,returnSize
);
return pRes
;
}
typedef struct TreeNode Node
;
void _InOrder(Node
* root
, int* pRes
, int* index
){
if (root
){
_InOrder(root
->left
, pRes
, index
);
pRes
[*index
] = root
->val
;
(*index
)++;
_InOrder(root
->right
, pRes
, index
);
}
}
int GetTreeSize(Node
* root
){
if (NULL == root
){
return 0;
}
return GetTreeSize(root
->left
) + GetTreeSize(root
->right
) + 1;
}
int* inOrderTraversal(struct Node
* root
, int* returnSize
){
*returnSize
= GetTreeSize(root
);
int* pRes
= (int*)malloc(sizeof(int*)(*returnSize
));
if (NULL == pRes
){
assret(0);
return NULL;
}
int index
= 0;
_Inorder(root
, pRes
, &index
);
return pRes
;
}
typedef struct TreeNode Node
;
void _PosOrder(Node
* pRoot
, int* pRes
, int* index
){
if (pRoot
){
_PosOrder(pRoot
->left
, pRes
, index
);
_PosOrder(pRoot
->right
, pRes
, index
);
pRes
[*index
] = pRoot
->val
;
(*index
)++;
}
}
int GetTreeSize(Node
* pRoot
){
if (NULL == pRoot
){
return 0;
}
return GetTreeSize(pRoot
->left
) + GetTreeSize(pRoot
->right
) + 1;
}
int* posorderTraversal(struct TreeNode
* root
, int* returnSize
){
*returnSize
= GetTreeSize(root
);
pRes
= (int*)malloc(sizeof(int)*(*returnSize
));
if (NULL == pRes
){
assert(0);
return NULL;
}
int index
= 0;
_PreOrder(root
, pRes
, returnSize
);
return pRes
;
}
二叉树的面试题(C++版本)
#include <iostream>
using namespace std
;
#if 0
void HeapAdjust(int* array
, int size
, int parent
)
{
int child
= parent
* 2 + 1;
while (child
< size
)
{
if (child
+1 < size
&& array
[child
+1] > array
[child
])
{
child
+= 1;
}
if (array
[parent
] < array
[child
])
{
swap(array
[parent
], array
[child
]);
parent
= child
;
child
= parent
* 2 + 1;
}
else
return;
}
}
void HeapSort(int* array
, int size
)
{
int root
= ((size
- 2) >> 1);
for (; root
>= 0; --root
)
HeapAdjust(array
, size
, root
);
int end
= size
- 1;
while (end
)
{
swap(array
[0], array
[end
]);
HeapAdjust(array
, end
, 0);
--end
;
}
}
int main()
{
int array
[] = {3,4,1,9,0,6,8,5,2,7};
HeapSort(array
, sizeof(array
) / sizeof(array
[0]));
return 0;
}
#endif
#if 0
template<class T, class Compare>
void HeapAdjust(T
* array
, int size
, int parent
, Compare com
)
{
int child
= parent
* 2 + 1;
while (child
< size
)
{
if (child
+ 1 < size
&& com(array
[child
+ 1], array
[child
]))
{
child
+= 1;
}
if (com(array
[child
], array
[parent
]))
{
swap(array
[parent
], array
[child
]);
parent
= child
;
child
= parent
* 2 + 1;
}
else
return;
}
}
template<class T, class Compare>
void HeapSort(T
* array
, int size
, Compare com
)
{
int root
= ((size
- 2) >> 1);
for (; root
>= 0; --root
)
HeapAdjust(array
, size
, root
, com
);
int end
= size
- 1;
while (end
)
{
swap(array
[0], array
[end
]);
HeapAdjust(array
, end
, 0, com
);
--end
;
}
}
#include <functional>
int main()
{
int array
[] = { 3, 4, 1, 9, 0, 6, 8, 5, 2, 7 };
HeapSort(array
, sizeof(array
) / sizeof(array
[0]), less
<int>());
return 0;
}
#endif
#include <vector>
struct BTNode
{
BTNode(int data
)
: _pLeft(nullptr)
, _pRight(nullptr)
, _data(data
)
{}
BTNode
* _pLeft
;
BTNode
* _pRight
;
int _data
;
};
BTNode
* CreateBinTree(const vector
<int>& v
, int& index
, const int invalid
)
{
BTNode
* pRoot
= nullptr;
if (index
< v
.size() && invalid
!= v
[index
])
{
pRoot
= new BTNode(v
[index
]);
pRoot
->_pLeft
= CreateBinTree(v
, ++index
, invalid
);
pRoot
->_pRight
= CreateBinTree(v
, ++index
, invalid
);
}
return pRoot
;
}
void Destroy(BTNode
*& pRoot
)
{
if (pRoot
)
{
Destroy(pRoot
->_pLeft
);
Destroy(pRoot
->_pRight
);
delete pRoot
;
pRoot
= nullptr;
}
}
void PreOrder(BTNode
* pRoot
)
{
if (pRoot
)
{
cout
<< pRoot
->_data
<< " ";
PreOrder(pRoot
->_pLeft
);
PreOrder(pRoot
->_pRight
);
}
}
#include <stack>
void PreOrderNor(BTNode
* pRoot
)
{
if (nullptr == pRoot
)
return;
stack
<BTNode
*> s
;
s
.push(pRoot
);
while (!s
.empty())
{
BTNode
* pCur
= s
.top();
s
.pop();
cout
<< pCur
->_data
<< " ";
if (pCur
->_pRight
)
s
.push(pCur
->_pRight
);
if (pCur
->_pLeft
)
s
.push(pCur
->_pLeft
);
}
}
void InOrder(BTNode
* pRoot
)
{
if (pRoot
)
{
InOrder(pRoot
->_pLeft
);
cout
<< pRoot
->_data
<< " ";
InOrder(pRoot
->_pRight
);
}
}
void InOrderNor(BTNode
* pRoot
)
{
if (nullptr == pRoot
)
return;
stack
<BTNode
*> s
;
BTNode
* pCur
= pRoot
;
while (!s
.empty() || pCur
)
{
while (pCur
)
{
s
.push(pCur
);
pCur
= pCur
->_pLeft
;
}
pCur
= s
.top();
cout
<< pCur
->_data
<< " ";
s
.pop();
pCur
= pCur
->_pRight
;
}
}
void PostOrder(BTNode
* pRoot
)
{
if (pRoot
)
{
PostOrder(pRoot
->_pLeft
);
PostOrder(pRoot
->_pRight
);
cout
<< pRoot
->_data
<< " ";
}
}
void PostOrderNor(BTNode
* pRoot
)
{
if (nullptr == pRoot
)
return;
stack
<BTNode
*> s
;
BTNode
* pCur
= pRoot
;
BTNode
* pPrev
= nullptr;
while (!s
.empty() || pCur
)
{
while (pCur
)
{
s
.push(pCur
);
pCur
= pCur
->_pLeft
;
}
BTNode
* pTop
= s
.top();
if (nullptr == pTop
->_pRight
||
pTop
->_pRight
== pPrev
)
{
cout
<< pTop
->_data
<< " ";
pPrev
= pTop
;
s
.pop();
}
else
{
pCur
= pTop
->_pRight
;
}
}
}
#include <queue>
void LevelOrder(BTNode
* pRoot
)
{
if (nullptr == pRoot
)
return;
queue
<BTNode
*> q
;
q
.push(pRoot
);
while (!q
.empty())
{
BTNode
* pCur
= q
.front();
cout
<< pCur
->_data
<< " ";
if (pCur
->_pLeft
)
q
.push(pCur
->_pLeft
);
if (pCur
->_pRight
)
q
.push(pCur
->_pRight
);
q
.pop();
}
}
size_t
Height(BTNode
* pRoot
)
{
if (nullptr == pRoot
)
return 0;
size_t leftHeight
= Height(pRoot
->_pLeft
);
size_t rightHeight
= Height(pRoot
->_pRight
);
return leftHeight
> rightHeight
? leftHeight
+ 1 : rightHeight
+ 1;
}
size_t
GetBTreeNode(BTNode
* pRoot
)
{
if (nullptr == pRoot
)
return 0;
return GetBTreeNode(pRoot
->_pLeft
) +
GetBTreeNode(pRoot
->_pRight
) + 1;
}
size_t
GetLeafNode(BTNode
* pRoot
)
{
if (nullptr == pRoot
)
return 0;
if (nullptr == pRoot
->_pLeft
&& nullptr == pRoot
->_pRight
)
{
return 1;
}
return GetLeafNode(pRoot
->_pLeft
) + GetLeafNode(pRoot
->_pRight
);
}
size_t
GetKLevelNodeCount(BTNode
* pRoot
, size_t K
)
{
if (nullptr == pRoot
|| 0 == K
)
return 0;
if (1 == K
)
return 1;
return GetKLevelNodeCount(pRoot
->_pLeft
, K
- 1) +
GetKLevelNodeCount(pRoot
->_pRight
, K
- 1);
}
BTNode
* GetParent(BTNode
* pRoot
, BTNode
* pNode
)
{
if (nullptr == pRoot
|| pRoot
== pNode
)
return nullptr;
if (pRoot
->_pLeft
== pNode
|| pRoot
->_pRight
== pNode
)
return pRoot
;
BTNode
* pParent
= GetParent(pRoot
->_pLeft
, pNode
);
if (pParent
)
return pParent
;
return GetParent(pRoot
->_pRight
, pNode
);
}
BTNode
* _ReBuildTree(const vector
<int>& pre
, int& index
, const vector
<int>& In
, int left
, int right
)
{
if (left
>= right
)
return nullptr;
BTNode
* pRoot
= new BTNode(pre
[index
]);
size_t InIdx
= left
;
while (In
[InIdx
] != pre
[index
])
InIdx
++;
if (left
< InIdx
)
pRoot
->_pLeft
= _ReBuildTree(pre
, ++index
, In
, left
, InIdx
);
if (index
+1 < right
)
pRoot
->_pRight
= _ReBuildTree(pre
, ++index
, In
, InIdx
+ 1, right
);
return pRoot
;
}
BTNode
* ReBuildTree(const vector
<int>& pre
, const vector
<int>& In
)
{
int index
= 0;
return _ReBuildTree(pre
, index
, In
, 0, In
.size());
}
int main()
{
vector
<int> v
{ 1, 2, 3, -1,-1,-1,4, 5, -1,-1,6 };
int index
= 0;
BTNode
* pRoot
= CreateBinTree(v
, index
, -1);
PreOrder(pRoot
);
PreOrderNor(pRoot
);
cout
<< endl
;
InOrder(pRoot
);
InOrderNor(pRoot
);
cout
<< endl
;
PostOrder(pRoot
);
PostOrderNor(pRoot
);
Destroy(pRoot
);
vector
<int> Pre
{ 1, 2, 3, 4, 5, 6 };
vector
<int> In
{3,2,1,5,4,6};
BTNode
* pNewRoot
= ReBuildTree(Pre
, In
);
return 0;
}
转载请注明原文地址: https://yun.8miu.com/read-19648.html