理解微软100题
来源:互联网 发布:社交网络 thunder 编辑:程序博客网 时间:2024/05/18 01:24
声明:取自 ”july“的“微软100题“,加上一些个人理解,欢迎拍砖。
原文地址:http://blog.csdn.net/v_july_v/article/details/6126406
学习微软100题笔记:
1.二元查找树变双向链表:
#include <stdio.h>#include <iostream>
struct BSTreeNode
{
int m_nValue;
BSTreeNode *m_pLeft;
BSTreeNode *m_pRight;
};
typedef BSTreeNode DoubleList;
DoubleList * pHead;
DoubleList * pListIndex;
void convertToDoubleList(BSTreeNode * pCurrent);
BSTreeNode* addBSTreeNode(BSTreeNode * & pCurrent, int value)
{
if (NULL == pCurrent)
{
pCurrent = new BSTreeNode();
pCurrent->m_pLeft = NULL;
pCurrent->m_pRight = NULL;
pCurrent->m_nValue = value;
}
else if( pCurrent->m_nValue > value )
{
pCurrent->m_pLeft = addBSTreeNode( pCurrent->m_pLeft, value );
}
else if ( pCurrent->m_nValue< value )
{
pCurrent->m_pRight = addBSTreeNode(pCurrent->m_pRight, value);
}
else
{
std::cout<<"重复加入节点"<< std::endl;
}
return pCurrent;
}
void ergodicBSTree( BSTreeNode *pCurrent )
{
if( NULL == pCurrent )
{
return;
}
if( NULL != pCurrent->m_pLeft )
{
ergodicBSTree( pCurrent->m_pLeft );
}
if( NULL != pCurrent->m_pRight )
{ergodicBSTree( pCurrent->m_pRight );
}
}
void convertToDoubleList( BSTreeNode *pCurrent )
{
pCurrent->m_pLeft = pListIndex;
if( NULL != pListIndex )
{
pListIndex->m_pRight = pCurrent;
}
else
{
pHead = pCurrent;
}
pListIndex = pCurrent;
std::cout << pCurrent->m_nValue << std::endl;
}
int
main( void )
{
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;
}
其中建立二叉树时是按照 c语言程序设计中(K&R)方法建立的,大同小异。
重点是利用递归转换为链表的过程。
一个全局指针pListIndex指向pCurrent的前一个节点,每次调用convertToDoubleList完成
pCurrent->m_pRight -> pListIndex;因为这里pCurrent不为NULL,然后再pListIndex->m_pLift 指向当前pCurrent
要先判断pListIndex是否为NULL,因为pListIndex开始时是赋值为NULL的,不能进行解引用操作。
新手没经验,欢迎指正错误,待更新。详细请见开头原博文 ,july大神。
0 0
- 理解微软100题
- 微软多动画理解
- 微软面试100题
- 微软数据结构100题
- 微软面试100题
- 微软100题-1
- 微软面试100题
- 微软100题总结
- 【微软100题】逆置链表
- 微软面试100题
- 微软100题
- 微软面试100题
- 微软100题--004
- 微软100题-009
- 微软100题,39题
- [转载]微软面试100题
- 微软面试100题链接
- 微软面试100题整理
- [Leetcode 61, Medium] Rotate List
- MyBatis单条插入后返回自增的id
- 黑马程序员————Java基础日常笔记---IO流二
- 【树形dp】hdu 2412 Party at Hali-Bula
- Day03-JavaScript高级编程
- 理解微软100题
- 如何使用Word2007发布csdn博文
- 求质因数问题
- [Leetcode 62, Medium] Unique Paths
- LightOJ1038-Race to 1 Again
- Android overlay系统
- ------------参数传递的四种形式----- URL,超链接,js,form表单
- github
- 数组及函数的应用篇