您的位置:首页 > 职场人生

二叉树的一些笔试面试题目

2013-10-01 10:51 351 查看
#include <iostream>
#include <vector>
#include <stack>
#include <queue>
using namespace std;
//二叉树

struct BiTNode
{
char data;
BiTNode *lchild,*rchild;
};
//根据前序遍历结果和中序遍历结果
void buildBiTFromArr(char* pre, char* mid, int left1, int left2,int len, BiTNode** node){
if ( pre== NULL || mid== NULL ||pre[left1]=='\0'&&mid[left2]=='\0' ||len == 0)
{
return;
}
(*node)->data = pre[left1];
(*node)->lchild= NULL;
(*node)->rchild= NULL;
if ( len==1)       //说明无子树
{
return;
}
int middle=-1;           //记录pre[left]在 mid中对应的位置
for ( int j=left2 ; j < len+left2 ; j++  ){
if( mid[j]== pre[left1]){
middle = j;
break;
}
}
if(middle ==-1)
exit(-1);

//重建左子树
int lLen = middle- left2;
if(lLen> 0){
BiTNode* lNode = new BiTNode;
(*node)->lchild= lNode;
buildBiTFromArr( pre, mid, left1+1,left2, lLen,&lNode );
}
//重建右子树
int rLen = len - lLen-1;
if ( rLen>0)
{
BiTNode* rNode = new BiTNode;
(*node)->rchild= rNode;
buildBiTFromArr( pre, mid, left1+lLen+1, middle+1, rLen,&rNode );
}
}
//中序遍历
void inOrder(BiTNode* node){
if ( node==NULL)
{
return;
}
inOrder(node->lchild);
cout<<node->data<<'\t';
inOrder(node->rchild);

}
//前序遍历的非递归实现
void preOrder2(BiTNode* node){
stack<BiTNode*> temp;
if ( node == NULL) {
cout<<"空树";
return;
}
while ( node != NULL || !temp.empty()){
while(node != NULL){
cout<<node->data<<'\t';
temp.push( node);
node = node->lchild;
}
node = temp.top();
temp.pop();
node= node->rchild;
}
}
//中序遍历的非递归实现
void inOrder2( BiTNode* node){
stack<BiTNode*> temp;
if ( node == NULL) {
cout<<"空树";
return;
}
while ( node != NULL || !temp.empty())
{
while(node != NULL){
temp.push( node);
node= node->lchild;
}
node = temp.top();
cout<<node->data<<'\t';
temp.pop();
node = node->rchild;
}

}
//后序遍历
void backOrder(BiTNode* node){
if ( node==NULL)
{
return;
}
backOrder(node->lchild);
backOrder(node->rchild);
cout<<node->data<<'\t';
}
void backOrder2(BiTNode* node){
stack<BiTNode*> temp;
if ( node == NULL) {
cout<<"空树";
return;
}
while( node != NULL || !temp.empty()){
while( node!= NULL){
temp.push( node);
node= node->lchild;
}
node= node->rchild;
}

}

//层序遍历
void levelOrder(BiTNode* node){
queue<BiTNode*> temp;
temp.push(node);
while( node!= NULL && !temp.empty()){
node = temp.front();
cout<< node->data<<'\t';
if( node->lchild != NULL)
temp.push(node->lchild);
if( node->rchild != NULL)
temp.push(node->rchild);
temp.pop();
}
}
//求二叉树中结点的最大距离
//nMaxDistence: 最大距离
//思路:分别求左右子树的最大距离
int maxDistence(BiTNode* node, int& nMaxDistence){

if ( node == NULL) { //节点为空,距离为-1
return -1;
}
int lDepth = maxDistence(node->lchild, nMaxDistence) + 1;
int rDepth = maxDistence(node->rchild, nMaxDistence) + 1;
nMaxDistence = nMaxDistence > (lDepth + rDepth) ?  \
nMaxDistence : (lDepth + rDepth);
return lDepth> rDepth? lDepth: rDepth;
}
//求二叉树最大深度和最小深度
int minDepth(BiTNode* node){
//判断是否是叶节点,根据是否是空指针判断
if ( node->lchild== NULL && node->rchild== NULL)
{
return 1;
}
if( node->lchild == NULL)
return minDepth(node->rchild)+1;
if ( node->rchild ==NULL)
return minDepth(node->lchild);
int lMin= 0, rMin= 0;
lMin = minDepth(node->lchild);
rMin = minDepth(node->rchild);
return lMin < rMin ? (lMin+1) :( rMin + 1);
}
int maxDepth(BiTNode* node){
if ( node->lchild== NULL && node->rchild== NULL)
{
return 1;
}
if( node->lchild == NULL)
return maxDepth(node->rchild)+1;
if ( node->rchild ==NULL)
return maxDepth(node->lchild);
int lMin= 0, rMin= 0;
lMin = maxDepth(node->lchild);
rMin = maxDepth(node->rchild);
return lMin > rMin ? (lMin+1) :( rMin + 1);
}

//4. 在二叉树中找出和为某一值的所有路径
void findAllPaths( BiTNode* head , int n, vector<int> path){
if ( head == NULL)
return;

if ( head->lchild== NULL && head->rchild== NULL && n== head->data) {
path.push_back(head->data);
for ( int i = 0; i < path.size(); i++ ){
cout<< path[i]<<'\t';
}
cout<<endl;
return;
}
path.push_back(head->data);
findAllPaths( head->lchild, n- head->data, path);
findAllPaths( head->rchild, n- head->data, path);
}

//指定二叉树,给定两节点求其最近共同父节点
//函数返回检测到node1或者node2的个数,检测到一个记为1,检测2个记为2
//最先检测到两个的是父节点,将父节点记录,并返回3,以避免父节点的父节点再次记录。

int findFatherNode(BiTNode* head, BiTNode* node1, BiTNode* node2,BiTNode** pFather){

if (head == NULL) return 0;
int state = 0;        //用于标记是否检测到node1或者node2,检测到+1
if (head == node1) state++;
if (head == node2) state++;
state+= findFatherNode( head->lchild, node1, node2, pFather);
state+= findFatherNode( head->rchild, node1, node2, pFather);
if ( state == 2) {
*pFather = head;
state = 3;
}
return state;
}

int main()
{
char pre[]="abdhkecfigj";
char mid[]="hkdbeaifcgj";
BiTNode* head= new BiTNode;
buildBiTFromArr(pre, mid, 0,0,sizeof(pre)-1, &head);
levelOrder(head);
cout<<endl;
cout<<minDepth(head)<<endl;
cout<<maxDepth(head)<<endl;

int n= 'a'+'b'+'g';
vector<int> temp;
findAllPaths(head, n, temp);

int mDistence;
maxDistence(head, mDistence);
cout<<"最大距离为:"<<mDistence;

//find father node
BiTNode* node1 = head->lchild->lchild;
BiTNode* node2 = head->rchild->lchild->lchild;
BiTNode* fatheNode = new BiTNode;
findFatherNode(head, node1, node2, &fatheNode);
cout<<"father:"<<fatheNode->data;
system("pause");
return 0;
}


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: