华为公司笔试题:写出多叉树转化为二叉树的算法?

musehilt 2006-11-08 09:44:46
写出多叉树转化为二叉树的算法?注意:一棵树转化为二叉树的方法是:树的根结点为二叉树的根,树的结点的第一个儿子变成二叉树对应结点的左孩子,树的结点的右兄弟变成二叉树种该结点的右孩子。
有谁能帮忙写一个算法吗?谢谢了
...全文
1277 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
ysc918 2006-11-08
  • 打赏
  • 举报
回复
typedef struct BinaryTreeNode{
struct BinaryTreeNode* leftChild;
struct BinaryTreeNode* rightChild;
int value;
};

typedef struct TreeNode{
struct TreeNode* child[];
int child_count;
int value;
};


BinaryTreeNode* ToBinaryTree(TreeNode* root){
if(root == null)
    return null;
BinaryTreeNode* binaryRoot = new BinaryTreeNode();
binaryRoot->value = root->value;
binaryRoot->leftChild = ToBinaryTree(root->child[0]);
BinaryTreeNode* brother = binaryRoot->leftChild;
for(int i = 1; i < root->child_count;i++){
brother->rightChild = ToBinaryTree(root->child[i]);
brother = brother->rightChild;
}
return binaryRoot;
}
Bennyatt 2006-11-08
  • 打赏
  • 举报
回复
买个算法分析书
或者是 数据结构 书
有现成的 。。
要不就 baidu搜索代码
morphymorphy 2006-11-08
  • 打赏
  • 举报
回复
具体实现依赖树的存储结构和表示方式。。
不一而足。。
gaopl_8278 2006-11-08
  • 打赏
  • 举报
回复
树根为二叉树的根结点,树的第一层最左结点为二叉树的左子树结点,为二叉树新建一个右子绎结点,树的所有第一层除最左结点外的结点都放在这个新结点下,以后依些类推.
Dan1980 2006-11-08
  • 打赏
  • 举报
回复
感觉这个题出的有问题耶。
多叉树的存储结构有不止一种。多叉树如果采用“长子-右兄弟”的方式存储,那本身就是一棵二叉树,不存在物理转换,只存在逻辑转换,也就不存在什么算法了。

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧