zoukankan      html  css  js  c++  java
  • 微软面试100 题题解

     1 /*
     2     Name: 在二元树中找出和为某一值的所有路径(树)
     3     Copyright: 
     4     Author: yifi
     5     Date: 27/11/16 17:10
     6     Description: 
     7 */
     8 
     9 #include <bits/stdc++.h>
    10 
    11 using namespace std;
    12 
    13 
    14 struct BinaryTreeNode
    15 {
    16     int m_nValue;
    17     BinaryTreeNode * m_pLeft;
    18     BinaryTreeNode * m_pRight;
    19 };
    20 
    21 int count = 0;
    22 
    23 
    24 void FindPath(BinaryTreeNode* pTreeNode,
    25 int expectedSum,vector<int>& path,int& currentSum)
    26 {
    27     if (!pTreeNode)
    28     {
    29         return ;
    30     }
    31     currentSum += pTreeNode->m_nValue;
    32     path.push_back(pTreeNode->m_nValue);
    33     bool isLeaf = !(pTreeNode->m_pLeft) && !(pTreeNode->m_pRight);
    34     
    35     
    36     if (currentSum == expectedSum && isLeaf)
    37     {
    38         vector<int>::iterator iter;
    39         for (iter = path.begin();iter != path.end(); iter++)
    40         {
    41             cout << *iter << "	"; 
    42         }
    43         cout << endl;
    44     }
    45     
    46     if (pTreeNode->m_pLeft)
    47     {
    48         FindPath(pTreeNode->m_pLeft,expectedSum,path,currentSum);
    49     }
    50     if (pTreeNode->m_pRight)
    51     {
    52         FindPath(pTreeNode->m_pRight,expectedSum,path,currentSum);
    53     }
    54     currentSum -= pTreeNode->m_nValue;
    55     path.pop_back();
    56 }
    57 
    58 void addTree(BinaryTreeNode **T, int num)
    59 {
    60     if(*T == NULL)
    61     {   
    62         *T = (BinaryTreeNode *)malloc(sizeof(BinaryTreeNode));
    63         (*T)->m_nValue = num;
    64         (*T)->m_pLeft = NULL;
    65         (*T)->m_pRight = NULL;
    66     }   
    67     else if((*T)->m_nValue > num)
    68          addTree(&((*T)->m_pLeft), num);
    69     else if((*T)->m_nValue < num)
    70         addTree(&((*T)->m_pRight), num);
    71     else
    72         cout << "重复加入同一结点" << endl;
    73 }
    74 
    75  int main()
    76 {
    77     BinaryTreeNode * T = NULL;
    78     addTree(&T, 10);
    79     addTree(&T, 12);
    80     addTree(&T, 5);
    81     addTree(&T, 7);
    82     addTree(&T, 4);
    83 
    84     vector<int> path;
    85     int sum = 0;
    86     FindPath(T, 22, path, sum);
    87 
    88     return 0;
    89 }
    90              
    在二元树中找出和为某一值的所有路径(树)
    /*
        Name: 子数组的最大和(数组)
        Copyright: 
        Author: yifi
        Date: 23/11/16 22:51
        Description: 
    */
    
    
    #include <bits/stdc++.h>
    
    using namespace std;
    
    int main()
    {
        int sum = 0;
        int MaxSum = -10000000;
        int a[] = {1,-2,3,10,-4,7,2,-5};
        int i = 0;
        for (i = 0;i<8;i++)
        {
            if (sum < 0)
            {
                sum = a[i];
            }
            else 
            {
                sum += a[i];
            }
            if (sum > MaxSum)
            {
                MaxSum = sum;
            }
        }
        cout << MaxSum << endl;
    }
    子数组的最大和(数组)
    /*
        Name: 包含 min 函数的栈(栈)
        Copyright: 
        Author: yifi 
        Date: 23/11/16 22:16
        Description: 
    */
    
    #include <stdio.h>
    
    #include <iostream>
    using namespace std;
    
    #define STACK_SIZE 20
    
    typedef struct _STACK{
        int data[STACK_SIZE];
        int top;
        int min;
    }Stack;
    
    void Init(Stack* s);
    void Push(Stack* s,int val);
    int Pop(Stack* s);
    bool Full(Stack* s);
    bool Empty(Stack* s);
    int Min(Stack* s);
    
    
    Stack haha;
    int main()
    {
        Stack s;
       Init( &s );
       Push( &s, 3);
       std::cout << Min( &s ) << std::endl;
       Push( &s, 4);
       std::cout << Min( &s ) << std::endl;
       Push( &s, 2);
       std::cout << Min( &s ) << std::endl;
       Push( &s, 1);
       std::cout << Min( &s ) << std::endl;
       Pop( &s );
       std::cout << Min( &s ) << std::endl;
       Pop( &s );
       std::cout << Min( &s ) << std::endl;
       Push( &s, 0);
       std::cout << Min( &s ) << std::endl;
       return 0;
    }
    
    void Init(Stack* s)
    {
        s->top = -1;
        s->min = -1;
        haha.top = -1;
    }
    
    
    void Push(Stack* s,int val)
    {
        if (Full(s))
        {
            printf("Satck is Full
    ");
            return ;
        }
        if (s->min == -1 || val < s->min)
        {
            s->min = val;
            haha.data[++haha.top] = val;
        }
        s->data[++s->top] = val;
    }
    
    
    
    int Pop( Stack *s )
    {
       int data;
       if( Empty(s) )
       {
          printf( "stack is empty
    " );
          return -1;
       }
       data = s->data[s->top--];
       if( data == haha.data[haha.top] )
       {
          s->min = haha.data[--haha.top];
       }
       return data;
    }
    int Min( Stack *s )
    {
       return s->min;
    }
    bool Empty( Stack *s )
    {
       if( s->top == -1 )
       {
          return true;
       }
       return false;
    }
    
    bool Full( Stack *s )
    {
       if( s->top == STACK_SIZE-1 )
       {
          return true;
       }
       return false;
    }
    包含 min 函数的栈(栈)
     1 /*
     2     Name: 二元查找树转变成排序的双向链表(树)
     3     Copyright: 
     4     Author: yifi
     5     Date: 23/11/16 17:06
     6     Description: 
     7 */
     8 
     9 
    10 #include <iostream>
    11 using namespace std;
    12 
    13 typedef struct _BSTreeNode{
    14     int m_Value;
    15     _BSTreeNode* pLeft;
    16     _BSTreeNode* pRight;
    17 }BSTreeNode,*pBSTreeNode; 
    18 
    19 void addBSTreeNode(BSTreeNode *&pCurrent,int value);
    20 void inOrderBSTree(BSTreeNode* pBSTree);
    21 void convertToDoubleList(BSTreeNode* pCurrent);
    22 
    23 BSTreeNode *pHead=NULL;//指向循环队列头结点
    24 BSTreeNode *pIndex=NULL;//指向前一个结点
    25 int main()
    26 {
    27     BSTreeNode *pRoot=NULL;
    28     addBSTreeNode(pRoot,10);
    29     addBSTreeNode(pRoot,6);
    30     addBSTreeNode(pRoot,14);
    31     addBSTreeNode(pRoot,4);
    32     addBSTreeNode(pRoot,8);
    33     addBSTreeNode(pRoot,12);
    34     addBSTreeNode(pRoot,16);
    35     inOrderBSTree(pRoot);
    36     return 0;
    37 }
    38 
    39 
    40 void addBSTreeNode(BSTreeNode *&pCurrent,int value)
    41 {
    42     if (pCurrent == NULL)     
    43     {
    44         BSTreeNode* pTemp = new BSTreeNode();
    45         pTemp->m_Value = value;
    46         pTemp->pLeft = NULL;
    47         pTemp->pRight = NULL;
    48         pCurrent = pTemp;
    49     }
    50     else if (pCurrent->m_Value < value)
    51     {
    52         addBSTreeNode(pCurrent->pRight,value);
    53     }
    54     else if (pCurrent->m_Value > value)
    55     {
    56         addBSTreeNode(pCurrent->pLeft,value);
    57     }
    58     else 
    59     {
    60         cout << "Node Repeated" << endl;
    61     }
    62 }
    63 
    64 void inOrderBSTree(BSTreeNode* pBSTree)
    65 {
    66     if (pBSTree == NULL)
    67     {
    68         return ;
    69     }
    70     if (pBSTree->pLeft != NULL)
    71     {
    72         inOrderBSTree(pBSTree->pLeft);
    73     }
    74      convertToDoubleList(pBSTree);
    75     if (pBSTree->pRight != NULL)
    76     {
    77         inOrderBSTree(pBSTree->pRight);
    78     }
    79      
    80 }
    81 void convertToDoubleList(BSTreeNode* pCurrent)
    82 {
    83     pCurrent->pLeft = pIndex;
    84     if (pIndex == NULL)
    85     {
    86         pHead = pCurrent;
    87     }
    88     else 
    89     {
    90         pIndex->pRight = pCurrent;
    91     }
    92     pIndex = pCurrent;
    93     
    94     cout << pCurrent->m_Value << " "; 
    95 } 
    二元查找树转变成排序的双向链表(树)
    爱程序 不爱bug 爱生活 不爱黑眼圈 我和你们一样 我和你们不一样 我不是凡客 我要做geek
  • 相关阅读:
    全选、反选和不选
    树状图
    年月日选择器
    细谈HTML5
    再谈HTML
    FlashFXP 破解代码
    文件上传示例
    PHP聊天室框架
    JS判断是否来自手机移动端的访问,并跳转
    JQUERY知识总结
  • 原文地址:https://www.cnblogs.com/yifi/p/6095822.html
Copyright © 2011-2022 走看看