微软面试100题1-10

来源:互联网 发布:品牌网络推广计划书 编辑:程序博客网 时间:2024/05/21 10:33
1.把二元查找树转变成排序的双向链表
题目:
输入一棵二元查找树,将该二元查找树转换成一个排序的双向链表。
要求不能创建任何新的结点,只调整指针的指向。
  
   10
   / /
  6  14
/ / / /
4  8 12 16
  
转换成双向链表

4=6=8=10=12=14=16。

分析:1、利用二元查找树的特点:左子树上的最大的数是整个左子树最右边的数(这个数是小于根节点的数中最大的数),右子树上最小的数是整个右子树上最左边的数(这个数是大于根节点的数中最小的数)。
      2、虽热不可以创建新的节点,但可以创建指向节点的指针,来记录一些特殊的节点(如左子树的最右节点,右子树的最左节点,父节点等等)位置。
      3、根节点时分为左右两部分处理。左处理部分可以用递归的方法(因为一次处理后形成的子树的根节点的左节点没有右节点,所以可以直接对新子树的根节点的左节点递归左处理),右处理部分也可以用递归的方法,左右两部分类似,是对应的。
 
 
“左处理”大体流程可以是这样的:1、从根节点开始,找到根节点的左子树的最右节点(假设用mark指向),找到以mark为根节点的子树的最左节点(用mark_left指向),用mark_parent指向mark的父节点。
                                2、假如mark就是根节点的左节点,直接将mark的右指针指向根节点即可;否则将mark_left的左指针指向根节点的左节点,mark的右指针指向根节点,根节点的左指针指向mark节点,并将mark_parent的右指针指向NULL。
                                3、终止条件是根节点的左指针为NULL(mark为NULL)。

#include <stdio.h>#include <iostream.h>struct BSTreeNode{    int m_nValue; // value of node    BSTreeNode *m_pLeft; // left child of node    BSTreeNode *m_pRight; // right child of node};typedef BSTreeNode DoubleList;DoubleList * pHead;DoubleList * pListIndex;void convertToDoubleList(BSTreeNode * pCurrent);// 创建二元查找树void addBSTreeNode(BSTreeNode * & pCurrent, int value){    if (NULL == pCurrent)    {        BSTreeNode * pBSTree = new BSTreeNode();        pBSTree->m_pLeft = NULL;        pBSTree->m_pRight = NULL;        pBSTree->m_nValue = value;        pCurrent = pBSTree;    }    else     {        if ((pCurrent->m_nValue) > value)        {            addBSTreeNode(pCurrent->m_pLeft, value);        }        else if ((pCurrent->m_nValue) < value)        {            addBSTreeNode(pCurrent->m_pRight, value);        }        else        {            //cout<<"重复加入节点"<<endl;        }    }}// 遍历二元查找树  中序void ergodicBSTree(BSTreeNode * pCurrent){    if (NULL == pCurrent)    {               return;    }    if (NULL != pCurrent->m_pLeft)    {        ergodicBSTree(pCurrent->m_pLeft);       }    // 节点接到链表尾部    convertToDoubleList(pCurrent);    // 右子树为空    if (NULL != pCurrent->m_pRight)    {        ergodicBSTree(pCurrent->m_pRight);    }}// 二叉树转换成listvoid  convertToDoubleList(BSTreeNode * pCurrent){    pCurrent->m_pLeft = pListIndex;    if (NULL != pListIndex)    {        pListIndex->m_pRight = pCurrent;    }    else    {        pHead = pCurrent;    }       pListIndex = pCurrent;    cout<<pCurrent->m_nValue<<endl;}int main(){    BSTreeNode * pRoot = NULL;    pListIndex = NULL;    pHead = NULL;    addBSTreeNode(pRoot, 10);    addBSTreeNode(pRoot, 4);    addBSTreeNode(pRoot, 6);    addBSTreeNode(pRoot, 8);    addBSTreeNode(pRoot, 12);    addBSTreeNode(pRoot, 14);    addBSTreeNode(pRoot, 15);    addBSTreeNode(pRoot, 16);    ergodicBSTree(pRoot);    return 0;}///////////////////////////////////////////////4681012141516Press any key to continue//////////////////////////////////////////////

--------------------------------------------------------------
同样是上道题,再给各位看一段简洁的代码:

void change(Node *p, Node *&last) //中序遍历{ if (!p)  return; change(p->left, last); if (last)  last->right = p; p->left = last;  last = p; change(p->right, last);}void main(){ Node *root = create(); Node *tail = NULL; change(root, tail); while (tail) {  cout << tail->data << " ";  tail = tail->left; } cout << endl;}--------------------------------------------------#include <iostream>using namespace std;class Node{public: int data; Node *left; Node *right;  Node(int d = 0, Node *lr = 0, Node *rr = 0):data(d), left(lr), right(rr){}};Node *create(){ Node *root; Node *p4 = new Node(4); Node *p8 = new Node(8); Node *p6 = new Node(6, p4, p8);  Node *p12 = new Node(12); Node *p16 = new Node(16); Node *p14 = new Node(14, p12, p16);  Node *p10 = new Node(10, p6, p14); root = p10;  return root;}Node *change(Node *p, bool asRight){ if (!p)  return NULL; Node *pLeft = change(p->left, false); if (pLeft)  pLeft->right = p; p->left = pLeft;  Node *pRight = change(p->right, true); if (pRight)  pRight->left = p; p->right = pRight;  Node *r = p; if (asRight) {  while (r->left)   r = r->left; }else{  while (r->right)   r = r->right; } return r;}void main(){ Node *root = create(); Node *tail = change(root, false); while (tail) {  cout << tail->data << " ";  tail = tail->left; } cout << endl;  root = create(); Node *head = change(root, true); while (head) {  cout << head->data << " ";  head = head->right; } cout << endl;}


0 0
原创粉丝点击