您的位置:首页 > 其它

笔试算法题(36):寻找一棵二叉树中最远节点的距离 & 根据二叉树的前序和后序遍历重建二叉树

2014-05-25 14:40 197 查看
出题:求二叉树中距离最远的两个节点之间的距离,此处的距离定义为节点之间相隔的边数;

分析:

最远距离maxDis可能并不经过树的root节点,而树中的每一个节点都可能成为最远距离经过的子树的根节点;所以计算出以每个节点为根节点的子树的最 远距离,最后取他们的最大值就是整棵树的最远距离;

如果递归层次过多造成系统栈溢出,则可以使用stack堆栈结构存储递归节点,从而使用循环实现

解题:

struct Node {
int value;
Node *left;
Node *right;
int leftDis;
int rightDis;
};

void MaxDistance(Node *root, int *MaxDis) {
/**
* 三个递归停止条件
* */
if(root==NULL)
return;
if(root->left==NULL)
root->leftDis=0;
if(root->right==NULL)
root->rightDis=0;

/**
* 处理当前节点之前,首先处理子节点
* */
if(root->left!=NULL)
MaxDistance(root->left, MaxDis);
if(root->right!=NULL)
MaxDistance(root->right, MaxDis);

/**
* 递归仅处理了root->left和root->right为根节点的
* 最远距离,所以当前已经知道root->left和root->right
* 各自的leftDis和rightDis
* */
if(root->left!=NULL) {
int tempMax=0;
if(root->left->leftDis > root->left->rightDis)
tempMax=root->left->leftDis;
else
tempMax=root->left->rightDis;
root->leftDis=tempMax+1;
}
if(root->right!=NULL) {
int tempMax=0;
if(root->right->leftDis > root->right->rightDis)
tempMax=root->right->leftDis;
else
tempMax=root->right->rightDis;
root->rightDis=tempMax+1;
}
/**
* 更新全局的最远距离MaxDis,最初调用的时候需要赋值为-1
* */
if(*MaxDis < root->leftDis + root->rightDis)
*MaxDis=root->leftDis + root->rightDis;
}


出题:如果已经知道一棵二叉树的前序和中序遍历结果,如何快速重建二叉树。如果知道前序和后序,或者知道中序和后序,是否仍旧可以重建;

分析:

下述为一个二叉树的例子,对应的前序,中序和后序遍历如下:

    Pre: abdehcfgi

    In: dbehafcig

    Suffix: dhebfigca


如果仅知道Pre和In,Pre序列的第一个字符必定为当前子树的根节点(a),所以对应到In序列中,可以根节点为分界(a)将左右子树分开(dbeh 和fcig),然后递归直到仅剩下一个字符;

Suffic和In也同理,但是仅知道Pre和Suffix不能重建二叉树。

解题:

struct Node {
int value;
Node *left;
Node *right;
};

Node* RestoreTree(char *pre, int pre1, int pre2,
char *in, int in1, int in2) {
if(pre1>pre2 || in1>in2) return NULL;
/**
* 当前pre的第一个字符必定是一棵子树的根节点
* 所以首先创建一个节点
* */
Node *temp=new Node();
temp->value=pre[pre1];
temp->left=NULL;
temp->right=NULL;

/**
* 当pre1和pre2相等时,说明已经只有一个字符
* 则说明二叉树已经到达子节点,直接返回
* */

if(pre1==pre2 || in1==in2)
return temp;

/**
* 查找pre[pre1]在in序列中的位置
* */
int i;
for(i=in1;i<=in2;i++) {
if(pre[pre1]==in[i])
break;
}

/**
* 对pre和in序列进行划分,注意当一个节点仅有左子节点
* 或者只有右子节点时,pre1可能大于pre2
* */
temp->left=RestoreTree(pre, pre1+1, pre1+(i-in1),
in, in1, i-1);
temp->right=RestoreTree(pre, pre1+(i-in1)+1, pre2,
in, i+1, in2);

return temp;
}

void showTree(Node *root) {
if(root==NULL)
return;

if(root->left!=NULL && root->right!=NULL) {
printf("%c, %c\n",root->left->value,
root->right->value);
showTree(root->left);
showTree(root->right);
} else if(root->left!=NULL) {
printf("%c\n",root->left->value);
showTree(root->left);
} else if(root->right!=NULL) {
printf("%c\n",root->right->value);
showTree(root->right);
}
}

int main() {
char pre[]="abdehcfgi";
char in[]="dbehafcig";

Node *root=RestoreTree(pre, 0, 8, in, 0, 8);
showTree(root);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐