日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

微软100题第11题

發布時間:2023/12/4 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 微软100题第11题 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

參照:http://blog.csdn.net/caryaliu/article/details/8107089

參照:http://blog.csdn.net/lalor/article/details/7626678

把二叉樹看成一個圖,父子節點之間的連線看成是雙向的,我們姑且定義"距離"為兩個節點之間的個數。

寫一個程序求一棵二叉樹中相距最遠的兩個節點之間的距離。

如下圖所示,粗箭頭的邊表示最長距離:


樹中相距最遠的兩個節點是A, B

分析可知:對于二叉樹,若要兩個節點U,V相距最遠,有兩種情況:

1,從U節點到V節點之間的路徑經過根節點

2,從U節點到V節點之間的路徑不經過根節點,這種情況下,U,V節點必定在根節點的左子樹或者右子樹上,這樣就轉化為求以根節點的孩子節點為根節點的二叉樹中最遠的兩個節點間的距離

上面所說的經過根節點,是指路徑中包含根節點,例如:加入上圖中只有左子樹FGHA, 那么最長距離的兩個節點是F, A,該路徑中包含根節點F,也稱為經過根節點。

于是我們可以遞歸求解,按照二叉樹的中序遍歷方式遍歷二叉樹,在遍歷的過程中尋找相距最遠的兩個節點。

解法1:

  • #include?<iostream>??
  • #include?<stdlib.h>??
  • using?namespace?std;??
  • ??
  • struct?BinaryTreeNode???
  • {??
  • ????int?m_nValue;??
  • ????struct?BinaryTreeNode?*m_pLeft;??
  • ????struct?BinaryTreeNode?*m_pRight;??
  • };??
  • ??
  • int?maxDistance(BinaryTreeNode?*root,?int?*max);??
  • int?DistanceCore(BinaryTreeNode?*root,int?*max);??
  • //后序遍歷,用于我們建立的二叉樹是否正確??
  • void?Traverse(?BinaryTreeNode?*?root);??
  • BinaryTreeNode*?Construct(int?*preorder,?int?*inorder,?int?lenght);??
  • BinaryTreeNode*?ConstructCore(int?*startPreorder,?int?*endPreorder,?int?*startInorder,?int?*endInorder);??
  • int?InsertNodeAtMostRight(BinaryTreeNode?*?root,?BinaryTreeNode?*?node);??
  • ??
  • ??
  • int?main(int?argc,?char*?argv[])??
  • {??
  • ??
  • ????int?preOrder[]?=?{5,?4,?8,?9,?6,?3,?18,?19,?2};??
  • ????int?inOrder[]?=?{9,?8,?6,?3,?4,?5,?19,?18,?2};??
  • ??
  • ????int?max;??
  • ??
  • ????//建樹??
  • ????BinaryTreeNode?*parent?=?Construct(preOrder,?inOrder,?sizeof(inOrder)?/?sizeof(inOrder[0]));??
  • ??
  • ????cout?<<?"A樹的后序遍歷的結果:"?<<?endl;??
  • ????Traverse(parent);??
  • ????cout?<<?endl;??
  • ??
  • ????BinaryTreeNode?*node1?=?(BinaryTreeNode?*)malloc(sizeof(BinaryTreeNode));??
  • ????BinaryTreeNode?*node2?=?(BinaryTreeNode?*)malloc(sizeof(BinaryTreeNode));??
  • ??
  • ????node1->m_nValue?=?0;??
  • ????node1->m_pLeft?=?NULL;??
  • ????node1->m_pRight?=?NULL;??
  • ??
  • ????node2->m_nValue?=?0;??
  • ????node2->m_pLeft?=?NULL;??
  • ????node2->m_pRight?=?NULL;??
  • ??
  • ????maxDistance(parent,?&max);??
  • ????cout?<<"max?distance?of?tree's?nodes?:?"?<<?max?<<?endl;??
  • ??
  • ????InsertNodeAtMostRight(parent,?node1);??
  • ????maxDistance(parent,?&max);??
  • ????cout?<<"max?distance?of?tree's?nodes?after?insert?node1:?"?<<?max?<<?endl;??
  • ??
  • ????InsertNodeAtMostRight(parent,?node2);??
  • ????maxDistance(parent,?&max);??
  • ????cout?<<"max?distance?of?tree's?nodes?after?insert?node2:?"?<<?max?<<?endl;??
  • ??
  • ????//測試極端情況,即只有一個節點??
  • ????maxDistance(node2,?&max);??
  • ????cout?<<"just?one?node?"?<<?max?<<?endl;??
  • ??
  • ????//測試極端情況,即只有二個節點??
  • ????maxDistance(node1,?&max);??
  • ????cout?<<"just?two?node?"?<<?max?<<?endl;??
  • ????return?0;??
  • }??
  • ??
  • BinaryTreeNode*?Construct(int?*preorder,?int?*inorder,?int?lenght)??
  • {??
  • ????if?(preorder?==?NULL?||?inorder?==?NULL?||?lenght?<=?0)???
  • ????{??
  • ????????return?NULL;??
  • ????}??
  • ????return?ConstructCore(preorder,?preorder?+?lenght?-?1,?inorder,?inorder?+?lenght?-?1);??
  • }??
  • ??
  • BinaryTreeNode*?ConstructCore(int?*startPreorder,?int?*endPreorder,?int?*startInorder,?int?*endInorder)??
  • {??
  • ????int?rootValue?=?startPreorder[0];??
  • ????BinaryTreeNode?*root?=?new?BinaryTreeNode();??
  • ????root->m_nValue?=?rootValue;??
  • ????root->m_pLeft?=?root->m_pRight?=?NULL;??
  • ??
  • ????if?(startPreorder?==?endPreorder)???
  • ????{//先序遍歷已經結束了,那這個時候一定是插入最后一個節點,則應該滿足下面的if語句,否則輸入的數據有誤??
  • ????????if?(startInorder?==?endInorder?&&?*startPreorder?==?*startInorder)???
  • ????????{??
  • ????????????return?root;??
  • ????????}??
  • ????????else??
  • ????????{??
  • ????????????cout?<<?"Invalid?input"?<<?endl;??
  • ????????????exit(-1);??
  • ????????}??
  • ????}??
  • ??
  • ????int?*rootInorder?=?startInorder;??
  • ????while?(rootInorder?<=?endInorder?&&?*rootInorder?!=?rootValue)???
  • ????{??
  • ????????++rootInorder;??
  • ????}??
  • ????if?(rootInorder?<=?endInorder?&&?*rootInorder?!=?rootValue)???
  • ????{??
  • ????????cout?<<?"Invalid?input"?<<?endl;??
  • ????????exit(-1);??
  • ????}??
  • ??
  • ????int?leftLength?=?rootInorder?-?startInorder;??
  • ????int?*leftPreorderEnd?=?startPreorder?+?leftLength;??
  • ??
  • ????if?(leftLength?>?0)???
  • ????{??
  • ????????root->m_pLeft?=?ConstructCore(startPreorder?+?1,?leftPreorderEnd,?startInorder,?rootInorder?-?1);??
  • ????}??
  • ????if?(leftLength?<?endPreorder?-?startPreorder)???
  • ????{??
  • ????????root->m_pRight?=?ConstructCore(leftPreorderEnd?+?1,?endPreorder,?rootInorder?+?1,?endInorder);??
  • ????}??
  • ??
  • ????return?root;??????
  • }??
  • ??
  • void?Traverse(?BinaryTreeNode?*?root)??
  • {??
  • ????if?(root?==?NULL)???
  • ????{??
  • ????????return;??
  • ????}??
  • ????else??
  • ????{??
  • ????????Traverse(root->m_pLeft);??
  • ????????Traverse(root->m_pRight);??
  • ????????cout?<<?root->m_nValue?<<?"??";??
  • ????}??
  • }??
  • int?maxDistance(BinaryTreeNode?*root,?int?*max)??
  • {??
  • ????//這個函數的主要功能是判斷root不為空,且給max賦初值??
  • ????if?(root?==?NULL?||?max?==?NULL)???
  • ????{??
  • ????????return?-1;??
  • ????}??
  • ????*max?=?0;??
  • ????return?DistanceCore(root,?max);??
  • }??
  • ??
  • int?DistanceCore(BinaryTreeNode?*root,?int?*max)??
  • {??
  • ????//如果節點是葉子節點,則返回0——深度??
  • ????if?(root->m_pLeft?==?NULL?&&?root->m_pRight?==?NULL)???
  • ????{??
  • ????????return?0;??
  • ????}??
  • ??
  • ????//保存左右子樹的最大深度??
  • ????int?lDistance?=?0;??
  • ????int?rDistance?=?0;??
  • ??
  • ????//左子樹不為空,返回當前節點到左子樹的最大深度??
  • ????if?(root->m_pLeft?!=?NULL)???
  • ????{??
  • ????????lDistance?=?1?+?DistanceCore(root->m_pLeft,?max);??
  • ????}??
  • ??
  • ????if?(root->m_pRight?!=?NULL)???
  • ????{??
  • ????????rDistance?=?1?+?DistanceCore(root->m_pRight,?max);??
  • ????}??
  • ??
  • ????//遍歷到當前節點時,能獲得的最大距離??
  • ????if?(lDistance?+?rDistance?>?*max)???
  • ????{??
  • ????????//保存當前獲得的最大距離??
  • ????????*max?=?lDistance?+?rDistance;??
  • ????}??
  • ????//返回左右子樹中,深度較大的一個??
  • ????return?lDistance?>?rDistance???lDistance?:?rDistance;??
  • }??
  • ??
  • ??
  • //為了測試程序寫的輔助函數,在樹的最最右邊插入一個新的節點??
  • int?InsertNodeAtMostRight(BinaryTreeNode?*?root,?BinaryTreeNode?*?node)??
  • {??
  • ????if?(root?==?NULL?||?node?==?NULL)???
  • ????{??
  • ????????return?-1;??
  • ????}??
  • ??
  • ????while?(root->m_pRight?!=?NULL)???
  • ????{??
  • ????????root?=?root->m_pRight;??
  • ????}??
  • ??
  • ????root->m_pRight?=?node;??
  • ????return?0;??
  • } ?
  • 解法2:

  • #include?<stdlib.h>??
  • #include?<stdio.h>??
  • ??
  • typedef?struct?Node?{??
  • ????struct?Node?*pleft;?????//左孩子??
  • ????struct?Node?*pright;????//右孩子??
  • ????char?chValue;???????????//該節點的值??
  • ??
  • ????int?leftMaxValue;???????//左子樹最長距離??
  • ????int?rightMaxValue;??????//右子樹最長距離??
  • }LNode,?*BinTree;??
  • ??
  • void?findMaxLen(BinTree?root,?int?*maxLen)?{??
  • ????//遍歷到葉子結點,返回??
  • ????if(root?==?NULL)??
  • ????????return;??
  • ??
  • ????//如果左子樹為空,那么該節點左邊最長距離為0??
  • ????if(root->pleft?==?NULL)??
  • ????????root->leftMaxValue?=?0;??
  • ??
  • ????//如果右子樹為空,那么該節點右邊最長距離為0??
  • ????if(root->pright?==?NULL)??
  • ????????root->rightMaxValue?=?0;??
  • ??
  • ????//如果左子樹不為空,遞歸尋找左子樹最長距離??
  • ????if(root->pleft?!=?NULL)??
  • ????????findMaxLen(root->pleft,?maxLen);??
  • ??
  • ????//如果右子樹不為空,遞歸尋找右子樹最長距離??
  • ????if(root->pright?!=?NULL)??
  • ????????findMaxLen(root->pright,?maxLen);??
  • ??
  • ????//計算左子樹中距離根節點的最長距離??
  • ????if(root->pleft?!=?NULL)?{??
  • ????????if(root->pleft->leftMaxValue?>?root->pleft->rightMaxValue)??
  • ????????????root->leftMaxValue?=?root->pleft->leftMaxValue?+?1;??
  • ????????else??
  • ????????????root->leftMaxValue?=?root->pleft->rightMaxValue?+?1;??
  • ????}??
  • ??
  • ????//計算右子樹中距離根節點的最長距離??
  • ????if(root->pright?!=?NULL)?{??
  • ????????if(root->pright->leftMaxValue?>?root->pright->rightMaxValue)??
  • ????????????root->rightMaxValue?=?root->pright->leftMaxValue?+?1;??
  • ????????else??
  • ????????????root->rightMaxValue?=?root->pright->rightMaxValue?+?1;??
  • ????}??
  • ??
  • ????//更新最長距離??
  • ????if(root->leftMaxValue?+?root->rightMaxValue?>?*maxLen)??
  • ????????*maxLen?=?root->leftMaxValue?+?root->rightMaxValue;??
  • }??
  • ??
  • //創建二叉樹??
  • void?buildBinTree(BinTree?*root)??
  • {??
  • ????char?ch;??
  • ????scanf("%c",?&ch);????//輸入一個元素??
  • ????fpurge(stdin);??
  • ????if(ch?==?'?')????????//若輸入的是空格符,表明二叉樹為空,置*root為NULL??
  • ????????*root?=?NULL;??
  • ????else?{???????????????//若輸入的不是空格符,則將該值賦值給根節點的chValue,?遞歸建立左子樹和右子樹??
  • ????????*root?=?(BinTree)malloc(sizeof(LNode));??
  • ????????(*root)->chValue?=?ch;??
  • ????????(*root)->leftMaxValue?=?0;??
  • ????????(*root)->rightMaxValue?=?0;??
  • ??
  • ????????buildBinTree(&(*root)->pleft);??
  • ????????buildBinTree(&(*root)->pright);??
  • ????}??
  • }??
  • ??
  • //銷毀二叉樹,釋放內存??
  • void?destroyBinTree(BinTree?*root)??
  • {??
  • ????if(*root?!=?NULL)?{??
  • ????????destroyBinTree(&(*root)->pleft);??
  • ????????destroyBinTree(&(*root)->pright);??
  • ??
  • ????????free(*root);??
  • ????????*root?=?NULL;??
  • ????}??
  • }??
  • ??
  • //前序遍歷二叉樹??
  • void?preOrderTraverse(BinTree?root)??
  • {??
  • ????if(root?!=?NULL)?{??
  • ????????preOrderTraverse(root->pleft);??
  • ????????printf("%c",?root->chValue);??
  • ????????preOrderTraverse(root->pright);??
  • ????}??
  • }??
  • ??
  • int?main()?{??
  • ????BinTree?root;??
  • ????buildBinTree(&root);??
  • ????preOrderTraverse(root);??
  • ????printf("\n");??
  • ????int?maxLen?=?0;??
  • ????findMaxLen(root,?&maxLen);??
  • ????printf("maxLen?=?%d\n",?maxLen);??
  • ????destroyBinTree(&root);??
  • } ?

  • 總結

    以上是生活随笔為你收集整理的微软100题第11题的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。