二叉树的构建、层次打印、广度遍历、深度遍历、K值路径

来源:互联网 发布:淘宝ipod classic 编辑:程序博客网 时间:2024/06/04 18:54

程序主要是二叉树方面的,第二面被鄙视了。下面对二叉树的面试题做个总结。

[cpp] view plaincopy
  1. #include <iostream>  
  2. #include <deque>  
  3. #include <vector>  
  4. using namespace std;  
  5.   
  6. struct BinaryTreeNode  
  7. {  
  8.     int m_nVal;  
  9.     BinaryTreeNode *m_pLeft;  
  10.     BinaryTreeNode *m_pRight;  
  11. };  
  12.   
  13. /* 
  14.     根据前序遍历和中序遍历构造二叉树 
  15. */  
  16. BinaryTreeNode* ConstructBinaryTreeCore(int *startPreOrder, int *endPreOrder, int *startInOrder, int *endInOrder)  
  17. {  
  18.     BinaryTreeNode *pRoot = new BinaryTreeNode;  
  19.   
  20.     pRoot->m_nVal = startPreOrder[0];  
  21.   
  22.     pRoot->m_pLeft = pRoot->m_pRight = NULL;  
  23.   
  24.     if (startPreOrder == endPreOrder)  
  25.     {  
  26.         if (startInOrder == endInOrder && *startInOrder == *endInOrder)  
  27.         {  
  28.             return pRoot;  
  29.         }  
  30.         else  
  31.         {  
  32.             return NULL;  
  33.         }  
  34.     }  
  35.   
  36.     int *current = startInOrder;  
  37.   
  38.     while (*current != startPreOrder[0])  
  39.     {  
  40.         ++current;  
  41.     }  
  42.   
  43.     int len = current - startInOrder;  
  44.   
  45.     if (len > 0)  
  46.     {  
  47.         pRoot->m_pLeft = ConstructBinaryTreeCore(startPreOrder+1, startPreOrder+len, startInOrder, current-1);  
  48.     }  
  49.   
  50.     if ((endInOrder-current) > 0)  
  51.     {  
  52.         pRoot->m_pRight = ConstructBinaryTreeCore(startPreOrder+len+1, endPreOrder, current+1, endInOrder);  
  53.     }  
  54.   
  55.     return pRoot;  
  56. }  
  57.   
  58. BinaryTreeNode* ConstructBinaryTree(int preOrder[], int inOrder[], int n)  
  59. {  
  60.     if (preOrder==NULL || inOrder==NULL || n<=0)  
  61.     {  
  62.         return NULL;  
  63.     }  
  64.   
  65.     return ConstructBinaryTreeCore(preOrder, preOrder+n-1, inOrder, inOrder+n-1);  
  66. }  
  67.   

  1. //求二叉树的深度  
  2. int GetDepth(tagBiNode *pRoot)  
  3. {  
  4.     if (pRoot == NULL)  
  5.     {  
  6.         return 0;  
  7.     }  
  8.   
  9.     //  int nLeftLength = GetDepth(pRoot->m_left);  
  10.     //  int nRigthLength = GetDepth(pRoot->m_right);  
  11.     //  return nLeftLength > nRigthLength ? (nLeftLength + 1) : (nRigthLength + 1);  
  12.   
  13.     return GetDepth(pRoot->left) > GetDepth(pRoot->right) ?   
  14.         (GetDepth(pRoot->left) + 1) : (GetDepth(pRoot->right) + 1);  
  15. }  
  16.   
  17. //求二叉树的宽度  
  18. int GetWidth(tagBiNode *pRoot)  
  19. {  
  20.     if (pRoot == NULL)  
  21.     {  
  22.         return 0;  
  23.     }  
  24.   
  25.     int nLastLevelWidth = 0;//记录上一层的宽度  
  26.     int nTempLastLevelWidth = 0;  
  27.     int nCurLevelWidth = 0;//记录当前层的宽度  
  28.     int nWidth = 1;//二叉树的宽度  
  29.     queue<BiNode *> myQueue;  
  30.     myQueue.push(pRoot);//将根节点入队列  
  31.     nLastLevelWidth = 1;      
  32.     tagBiNode *pCur = NULL;  
  33.   
  34.     while (!myQueue.empty())//队列不空  
  35.     {  
  36.         nTempLastLevelWidth = nLastLevelWidth;  
  37.         while (nTempLastLevelWidth != 0)  
  38.         {  
  39.             pCur = myQueue.front();//取出队列头元素  
  40.             myQueue.pop();//将队列头元素出对  
  41.   
  42.             if (pCur->left != NULL)  
  43.             {  
  44.                 myQueue.push(pCur->left);  
  45.             }  
  46.   
  47.             if (pCur->right != NULL)  
  48.             {  
  49.                 myQueue.push(pCur->right);  
  50.             }  
  51.   
  52.             nTempLastLevelWidth--;  
  53.         }  
  54.   
  55.         nCurLevelWidth = myQueue.size();  
  56.         nWidth = nCurLevelWidth > nWidth ? nCurLevelWidth : nWidth;  
  57.         nLastLevelWidth = nCurLevelWidth;  
  58.     }  
  59.   
  60.     return nWidth;  
  61. }  

  1. /* 
  2.     层次遍历二叉树 
  3. */  
  4. void PrintByLevel(BinaryTreeNode *pRoot)  
  5. {  
  6.     if (NULL == pRoot)  
  7.     {  
  8.         return ;  
  9.     }  
  10.   
  11.     deque<BinaryTreeNode *> dq;  
  12.     dq.push_back(pRoot);  
  13.   
  14.     while (dq.size() > 0)  
  15.     {  
  16.         BinaryTreeNode *pNode = dq.front();  
  17.         dq.pop_front();  
  18.         cout<<pNode->m_nVal<<endl;  
  19.   
  20.         if (pNode->m_pLeft != NULL)  
  21.         {  
  22.             dq.push_back(pNode->m_pLeft);  
  23.         }  
  24.   
  25.         if (pNode->m_pRight != NULL)  
  26.         {  
  27.             dq.push_back(pNode->m_pRight);  
  28.         }  
  29.     }  
  30. }  
  31.   
  32. /* 
  33.     二叉树的映像 
  34. */  
  35. void Mirror(BinaryTreeNode *pRoot)  
  36. {  
  37.     if (pRoot == NULL || (pRoot->m_pLeft==NULL && pRoot->m_pRight==NULL))  
  38.     {  
  39.         return ;  
  40.     }  
  41.   
  42.     BinaryTreeNode *pNode = pRoot->m_pLeft;  
  43.     pRoot->m_pLeft = pRoot->m_pRight;  
  44.     pRoot->m_pRight = pNode;  
  45.   
  46.     if (pRoot->m_pLeft != NULL)  
  47.     {  
  48.         Mirror(pRoot->m_pLeft);  
  49.     }  
  50.   
  51.     if (pRoot->m_pRight != NULL)  
  52.     {  
  53.         Mirror(pRoot->m_pRight);  
  54.     }  
  55. }  
  56.   
  57. /* 
  58.     打印二叉树中从根节点到叶子节点的节点值之和等于k的所有路径 
  59. */  
  60. void FindPathCore(BinaryTreeNode *pRoot, int sum, vector<int> path, int &curSum)  
  61. {  
  62.     curSum += pRoot->m_nVal;  
  63.     path.push_back(pRoot->m_nVal);  
  64.   
  65.     bool isLeaf = (pRoot->m_pLeft == NULL && NULL == pRoot->m_pRight);  
  66.   
  67.     if (isLeaf && curSum == sum)  
  68.     {  
  69.         cout<<"The Path is:"<<endl;  
  70.         for (vector<int>::iterator i=path.begin(); i!=path.end(); ++i)  
  71.         {  
  72.             cout<<*i<<endl;  
  73.         }  
  74.         cout<<endl;  
  75.     }  
  76.   
  77.     if (pRoot->m_pLeft != NULL)  
  78.     {  
  79.         FindPathCore(pRoot->m_pLeft, sum, path, curSum);  
  80.     }  
  81.   
  82.     if (pRoot->m_pRight != NULL)  
  83.     {  
  84.         FindPathCore(pRoot->m_pRight, sum, path, curSum);  
  85.     }  
  86.   
  87.     curSum -= pRoot->m_nVal;  
  88.     path.pop_back();  
  89. }  
  90.   
  91. void FindPath(BinaryTreeNode *pRoot, int sum)  
  92. {  
  93.     if (NULL == pRoot)  
  94.     {  
  95.         return ;  
  96.     }  
  97.   
  98.     vector<int> path;  
  99.     int curSum = 0;  
  100.     FindPathCore(pRoot, sum, path, curSum);  
  101. }  
  102.   
  103. int main()  
  104. {  
  105.     int preOrder[] = {1, 2, 5, 4, 6};  
  106.     int inOrder[] = {5, 2, 4, 1, 6};  
  107.   
  108.     BinaryTreeNode *pRoot = ConstructBinaryTree(preOrder, inOrder, 5);  
  109.     PrintByLevel(pRoot);  
  110.   
  111.     cout<<endl;  
  112.     Mirror(pRoot);  
  113.     PrintByLevel(pRoot);  
  114.   
  115.   
  116.     FindPath(pRoot, 7);  
  117.   
  118.     system("pause");  
  119.     return 0;  
  120. }  

翻转单词顺序

[cpp] view plaincopy
  1. #include <iostream>  
  2. using namespace std;  
  3.   
  4. void swap(char *a, char *b)  
  5. {  
  6.     char ch = *a;  
  7.     *a = *b;  
  8.     *b = ch;  
  9. }  
  10.   
  11. void Reverse(char *pBegin, char *pEnd)  
  12. {  
  13.     if (pBegin==NULL || NULL==pEnd)  
  14.     {  
  15.         return ;  
  16.     }  
  17.   
  18.     while (pBegin < pEnd)  
  19.     {  
  20.         swap(pBegin++, pEnd--);  
  21.     }  
  22. }  
  23.   
  24. char* ReverseSentence(char *pStr)  
  25. {  
  26.     if (pStr == NULL)  
  27.     {  
  28.         return NULL;  
  29.     }  
  30.   
  31.     char *pBegin = pStr;  
  32.     char *pEnd = pStr;  
  33.   
  34.     while (*pEnd != '\0')  
  35.     {  
  36.         pEnd++;  
  37.     }  
  38.     pEnd--;  
  39.   
  40.     Reverse(pBegin, pEnd);  
  41.   
  42.     pBegin = pEnd = pStr;  
  43.     while (*pBegin != '\0')  
  44.     {  
  45.         if (*pBegin == ' ')  
  46.         {  
  47.             pBegin++;  
  48.             pEnd++;  
  49.         }  
  50.         else if (*pEnd == ' ' || *pEnd == '\0')  
  51.         {  
  52.             Reverse(pBegin, --pEnd);  
  53.             pBegin = ++pEnd;  
  54.         }  
  55.         else  
  56.         {  
  57.             pEnd++;  
  58.         }  
  59.     }  
  60.   
  61.     return pStr;  
  62. }  
  63.   
  64. int main()  
  65. {  
  66.     char pStr[] = "I am a    huster.";  
  67.   
  68.     char *str = ReverseSentence(pStr);  
  69.     cout<<str<<endl;  
  70.   
  71.     system("pause");  
  72.     return 0;  
  73. }  
0 0