• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            SEMAN

            曾經(jīng)滄海難為水、除卻巫山不是云

            C++博客 首頁 新隨筆 聯(lián)系 聚合 管理
              9 Posts :: 3 Stories :: 24 Comments :: 0 Trackbacks

                今天參加MS2006年度秋季校園招聘會的筆試第三場,有一個算法題,求一個樹種兩個節(jié)點的最低公共節(jié)點,在網(wǎng)上Google了一下,看到原題大致這樣的:
                  Given the values of two nodes in a *binary search tree*, write a c program to find the lowest common ancestor. You may assume that both values already exist in the tree.

            The function prototype is as follows: int FindLowestCommonAncestor(node* root,int value1,int value)
                       20
                      /  \
                     8    22
                   /   \
                  4     12
                       /  \
                     10    14
                構(gòu)筑函數(shù): struct TreeNode * FindLowestCommonTreeNode(struct node *pNode,)

            Struct TreeNode
            {
               int Data;
               TreeNode *pLeft, *pRight;
            }

            FindLowestAncestor(Struct TreeNode *pRoot, Struct TreeNode *pNode1, Struct TreeNode *pNode2)
            {
               if (pRoot==NULL) 
                  return NULL;
               if (pRoot==pNode1 && pRoot==pNode2) 
                  return pRoot;
               Struct TreeNode *pTemp;
               if (pTemp = FindLowestAncestor(pRoot->pLeft,pNode1,pNode2)) 
                  return pTemp;
               if (pTemp = FindLowestAncestor(pRoot->pRight,pNode1,pNode2)) 
                  return pTemp;
               if (FindLowestAncestor(pRoot,pNode1,pNode1) && FindLowestAncestor(pRoot,pNo
            de2,pNode2)) return pRoot;

               return NULL;
            }

            posted on 2005-11-14 02:05 味全每日C++ 閱讀(1346) 評論(5)  編輯 收藏 引用

            Feedback

            # re: MS的筆試題目 2006-10-22 17:18 小小
            更多試題,請訪問: www.pghome.net/art.html  回復(fù)  更多評論
              

            # re: MS的筆試題目 2006-12-28 23:23 kgha
            FindLowestAncestor(Struct TreeNode *pRoot, Struct TreeNode *pNode1, Struct TreeNode *pNode2)
            {
            if (pRoot==NULL)
            return NULL;
            if (pRoot==pNode1 && pRoot==pNode2)
            return pRoot;
            Struct TreeNode *pTemp;
            if (pTemp = FindLowestAncestor(pRoot->pLeft,pNode1,pNode2))
            return pTemp;
            if (pTemp = FindLowestAncestor(pRoot->pRight,pNode1,pNode2))
            return pTemp;
            if (FindLowestAncestor(pRoot,pNode1,pNode1) && FindLowestAncestor(pRoot,pNo
            de2,pNode2)) return pRoot;

            return NULL;
            }

              回復(fù)  更多評論
              

            # re: MS的筆試題目 2006-12-28 23:37 kgha

            這樣太費解了,不如寫兩個函數(shù)直觀:
            FindLowestAncestor(Struct TreeNode *pRoot, Struct TreeNode *pNode1, Struct TreeNode *pNode2)
            {
            if (pRoot==NULL)
            return NULL;
            if (pRoot==pNode1 && pRoot==pNode2)
            return pRoot;
            Struct TreeNode *pTemp;
            if (pTemp = FindLowestAncestor(pRoot->pLeft,pNode1,pNode2))
            return pTemp;
            if (pTemp = FindLowestAncestor(pRoot->pRight,pNode1,pNode2))
            return pTemp;
            if (FindNode(pRoot,pNode1) && FindLowestAncestor(pRoot,pNo
            de2)) return pRoot;
            return NULL;
            }
            struct TreeNode * FindNode(Struct TreeNode *pRoot, Struct TreeNode *pNode)
            {
            if(pNode在pRoot下)
            {
            return pRoot;
            }
            return NULL;
            }
            因為最后一步僅僅是在pRoot下是否存在pNode1和pNode2,所以還有優(yōu)化的余地  回復(fù)  更多評論
              

            # re: MS的筆試題目 2006-12-28 23:41 kgha
            這樣太費解了,不如寫兩個函數(shù)直觀:
            FindLowestAncestor(Struct TreeNode *pRoot, Struct TreeNode *pNode1, Struct TreeNode *pNode2)
            {
            if (pRoot==NULL)
            return NULL;
            if (pRoot==pNode1 && pRoot==pNode2)
            return pRoot;
            Struct TreeNode *pTemp;
            if (pTemp = FindLowestAncestor(pRoot->pLeft,pNode1,pNode2))
            return pTemp;
            if (pTemp = FindLowestAncestor(pRoot->pRight,pNode1,pNode2))
            return pTemp;
            if (FindNode(pRoot,pNode1) && FindNode(pRoot,pNo
            de2)) return pRoot;
            return NULL;
            }
            struct TreeNode * FindNode(Struct TreeNode *pRoot, Struct TreeNode *pNode)
            {
            if(pNode在pRoot下)
            {
            return pRoot;
            }
            return NULL;
            }
            因為最后一步僅僅是在pRoot下是否存在pNode1和pNode2,所以還有優(yōu)化的余地
            上面的有點小錯誤,呵呵  回復(fù)  更多評論
              

            # re: MS的筆試題目 2008-03-07 17:31 521zheng
            其實沒有這么麻煩的,
            考慮一下二叉查找樹的特點,如果兩個節(jié)點的值都大于或都小于某一個節(jié)點的值,就繼續(xù)遍歷下去,否則返回節(jié)點的值
            int FindLowesCommonNode(root * node , int a, int b)
            {
            if(a<=node.value && b<=node.value)
            return FindLowesCommonNode(node->left, a,b);
            if(a>=node.value && b>=node.value)
            return FindLowesCommonNode(node->right, a,b);
            return root.value;
            }  回復(fù)  更多評論
              


            只有注冊用戶登錄后才能發(fā)表評論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


            亚洲性久久久影院| 久久综合久久鬼色| 色青青草原桃花久久综合| 四虎影视久久久免费| 看久久久久久a级毛片| 国产精品无码久久久久久| 99精品久久久久久久婷婷| 久久影院亚洲一区| 亚洲欧美日韩久久精品第一区| 青青青伊人色综合久久| 午夜人妻久久久久久久久| 狠色狠色狠狠色综合久久| 天堂久久天堂AV色综合| 久久精品成人免费看| 久久亚洲精品成人无码网站 | 狠狠色丁香婷婷久久综合| 中文字幕久久亚洲一区| 99久久国产热无码精品免费| 欧美日韩精品久久久免费观看| 欧美大香线蕉线伊人久久| 久久中文字幕视频、最近更新 | 国产亚洲婷婷香蕉久久精品| 久久无码中文字幕东京热| 久久精品18| 9191精品国产免费久久| 国产精品禁18久久久夂久| 久久久久久精品无码人妻| 久久夜色精品国产噜噜亚洲a| 伊人久久免费视频| 久久99精品久久久久久| 人妻少妇久久中文字幕| 97精品依人久久久大香线蕉97| 亚洲AV无码久久精品狠狠爱浪潮| 久久人妻少妇嫩草AV无码蜜桃| 久久人人爽人人爽人人AV| 伊人久久无码精品中文字幕| 精品国产91久久久久久久a| 久久久久九九精品影院| 国产AV影片久久久久久| 国内精品久久久久影院亚洲| 久久无码精品一区二区三区|