2014美團網校園招聘面試總結(華科)


2014年9月27日

程序主要是二叉樹方面的,第二面被鄙視了。下面對二叉樹的面試題做個總結。

#include <iostream>
#include <deque>
#include <vector>
using namespace std;

struct BinaryTreeNode
{
int m_nVal;
BinaryTreeNode *m_pLeft;
BinaryTreeNode *m_pRight;
};

/*
根據前序遍歷和中序遍歷構造二叉樹
*/
BinaryTreeNode* ConstructBinaryTreeCore(int *startPreOrder, int *endPreOrder, int *startInOrder, int *endInOrder)
{
BinaryTreeNode *pRoot = new BinaryTreeNode;

pRoot->m_nVal = startPreOrder[0];

pRoot->m_pLeft = pRoot->m_pRight = NULL;

if (startPreOrder == endPreOrder)
{
if (startInOrder == endInOrder && *startInOrder == *endInOrder)
{
return pRoot;
}
else
{
return NULL;
}
}

int *current = startInOrder;

while (*current != startPreOrder[0])
{
++current;
}

int len = current - startInOrder;

if (len > 0)
{
pRoot->m_pLeft = ConstructBinaryTreeCore(startPreOrder+1, startPreOrder+len, startInOrder, current-1);
}

if ((endInOrder-current) > 0)
{
pRoot->m_pRight = ConstructBinaryTreeCore(startPreOrder+len+1, endPreOrder, current+1, endInOrder);
}

return pRoot;
}

BinaryTreeNode* ConstructBinaryTree(int preOrder[], int inOrder[], int n)
{
if (preOrder==NULL || inOrder==NULL || n<=0)
{
return NULL;
}

return ConstructBinaryTreeCore(preOrder, preOrder+n-1, inOrder, inOrder+n-1);
}

/*
層次遍歷二叉樹
*/
void PrintByLevel(BinaryTreeNode *pRoot)
{
if (NULL == pRoot)
{
return ;
}

deque<BinaryTreeNode *> dq;
dq.push_back(pRoot);

while (dq.size() > 0)
{
BinaryTreeNode *pNode = dq.front();
dq.pop_front();
cout<<pNode->m_nVal<<endl;

if (pNode->m_pLeft != NULL)
{
dq.push_back(pNode->m_pLeft);
}

if (pNode->m_pRight != NULL)
{
dq.push_back(pNode->m_pRight);
}
}
}

/*
二叉樹的映像
*/
void Mirror(BinaryTreeNode *pRoot)
{
if (pRoot == NULL || (pRoot->m_pLeft==NULL && pRoot->m_pRight==NULL))
{
return ;
}

BinaryTreeNode *pNode = pRoot->m_pLeft;
pRoot->m_pLeft = pRoot->m_pRight;
pRoot->m_pRight = pNode;

if (pRoot->m_pLeft != NULL)
{
Mirror(pRoot->m_pLeft);
}

if (pRoot->m_pRight != NULL)
{
Mirror(pRoot->m_pRight);
}
}

/*
打印二叉樹中從根節點到葉子節點的節點值之和等於k的所有路徑
*/
void FindPathCore(BinaryTreeNode *pRoot, int sum, vector<int> path, int &curSum)
{
curSum += pRoot->m_nVal;
path.push_back(pRoot->m_nVal);

bool isLeaf = (pRoot->m_pLeft == NULL && NULL == pRoot->m_pRight);

if (isLeaf && curSum == sum)
{
cout<<"The Path is:"<<endl;
for (vector<int>::iterator i=path.begin(); i!=path.end(); ++i)
{
cout<<*i<<endl;
}
cout<<endl;
}

if (pRoot->m_pLeft != NULL)
{
FindPathCore(pRoot->m_pLeft, sum, path, curSum);
}

if (pRoot->m_pRight != NULL)
{
FindPathCore(pRoot->m_pRight, sum, path, curSum);
}

curSum -= pRoot->m_nVal;
path.pop_back();
}

void FindPath(BinaryTreeNode *pRoot, int sum)
{
if (NULL == pRoot)
{
return ;
}

vector<int> path;
int curSum = 0;
FindPathCore(pRoot, sum, path, curSum);
}

int main()
{
int preOrder[] = {1, 2, 5, 4, 6};
int inOrder[] = {5, 2, 4, 1, 6};

BinaryTreeNode *pRoot = ConstructBinaryTree(preOrder, inOrder, 5);
PrintByLevel(pRoot);

cout<<endl;
Mirror(pRoot);
PrintByLevel(pRoot);


FindPath(pRoot, 7);

system("pause");
return 0;
}

翻轉單詞順序

#include <iostream>
using namespace std;

void swap(char *a, char *b)
{
char ch = *a;
*a = *b;
*b = ch;
}

void Reverse(char *pBegin, char *pEnd)
{
if (pBegin==NULL || NULL==pEnd)
{
return ;
}

while (pBegin < pEnd)
{
swap(pBegin++, pEnd--);
}
}

char* ReverseSentence(char *pStr)
{
if (pStr == NULL)
{
return NULL;
}

char *pBegin = pStr;
char *pEnd = pStr;

while (*pEnd != '\0')
{
pEnd++;
}
pEnd--;

Reverse(pBegin, pEnd);

pBegin = pEnd = pStr;
while (*pBegin != '\0')
{
if (*pBegin == ' ')
{
pBegin++;
pEnd++;
}
else if (*pEnd == ' ' || *pEnd == '\0')
{
Reverse(pBegin, --pEnd);
pBegin = ++pEnd;
}
else
{
pEnd++;
}
}

return pStr;
}

int main()
{
char pStr[] = "I am a huster.";

char *str = ReverseSentence(pStr);
cout<<str<<endl;

system("pause");
return 0;
}


注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2021 ITdaan.com