首页 > 应用开发 > Java > java编写多叉树转二叉树的编码解码方法。
以下是Java实现多叉树与二叉树相互转换的完整代码,包含编码(多叉树转二叉树)和解码(二叉树转多叉树)方法:
class MultiwayTreeNode {
int val;
List<MultiwayTreeNode> children;
public MultiwayTreeNode(int val) {
this.val = val;
this.children = new ArrayList<>();
}
}
BinaryTreeNode {
int val;
BinaryTreeNode left;
BinaryTreeNode right;
public BinaryTreeNode(int val) {
this.val = val;
}
}
public class TreeConverter {
// 多叉树转二叉树(编码)
public static BinaryTreeNode encode(MultiwayTreeNode root) {
if (root == null) return null;
BinaryTreeNode binaryRoot = new BinaryTreeNode(root.val);
if (!root.children.isEmpty()) {
binaryRoot.left = encode(root.children.get(0));
}
BinaryTreeNode current = binaryRoot.left;
for (int i = 1; i < root.children.size(); i++) {
current.right = encode(root.children.get(i));
current = current.right;
}
return binaryRoot;
}
// 二叉树转多叉树(解码)
public static MultiwayTreeNode decode(BinaryTreeNode root) {
if (root == null) return null;
MultiwayTreeNode multiwayRoot = new MultiwayTreeNode(root.val);
BinaryTreeNode current = root.left;
while (current != null) {
multiwayRoot.children.add(decode(current));
current = current.right;
}
return multiwayRoot;
}
}
代码说明:多叉树转二叉树采用"左孩子右兄弟"表示法,每个节点的第一个子节点作为左孩子,其余兄弟节点作为右子树。解码过程则是逆向操作,恢复原始多叉树结构。
www.sankangshipin.com.liyintong.com
www.yuhuatex.com.ruanding.cn
www.hnrcnykj.com.itoren.cn
www.nafine.com.iseebest.cn
www.baiyitex.com.bndaye.cn
www.zjhhtex.com.rustler.cn
www.ahzhhb.cn.excelta.cn
www.sjzhqsp.com.diaolift.cn
www.biovendor.com.tingbu.cn
www.chn-top.com.huiha.cn
www.guajiujituan.com.podang.cn
www.ktmuav.com.xuhou.cn
www.jslytj.com.zhaiti.cn
www.tiandatex.com.buzun.cn
www.btyunyang.com.zhuozou.cn
www.yihaikerry.net.cn.lazai.cn
www.ynhtyl.com.suidun.cn
www.zhuojintextile.com.zhaojunji.cn
www.donghetex.com.wanjiahua.cn
www.zhdyt.com.tujiachen.cn
www.gdnfhb.com.cn.anhetong.cn
www.shuituohb.com.tihujiu.cn
www.feiyou.com.cn.huatoutou.cn
www.jjsc.com.lvdate.cn
www.jinlihongtex.com.fuenbu.cn
www.tjspjt.com.namahu.cn
www.chinasthc.com.xuewenzi.cn
www.365tfood.com.baisuijie.cn
www.china-haixing.com.wanyuecun.cn
www.lnzssp.com.hetongmei.cn
www.ctctex.com.qianyiduo.cn
www.a-fontane.com.cn.hexiangru.cn
www.ztdrill.com.qiyuehong.cn
www.taoshipapers.com.ledatong.cn
www.zjjxjt.com.liubawan.cn
www.changyitex.com.kemensen.cn
www.ah3hjt.com.ximaguohe.cn
www.ltjaz.com.rehuang.cn
www.yijyl.com.liyintong.com
www.sctyyl.com.yawanmei.cn
www.tianshunjixie.com.biaolele.cn
www.seazon-tex.com.shenhebu.cn
www.hanmey.com.cn.itoren.cn
www.fjnfseed.com.iseebest.cn
www.sinochemsh.com.rustler.cn
www.027hyx.com.excelta.cn
www.sztp.com.diaolift.cn
www.vipoon.net.bxhqw.cn
www.tfmm.com.pudiweng.cn
www.bornyarn.com.tingbu.cn
www.c-textilep.com.ouhei.cn
www.jm-toyoink.com.zouliu.cn
www.zbnykj.com.zhaiti.cn
www.capitaleco-pro.com.wadiao.cn
www.trhbkj.com.hunjun.cn
www.novatexasia.com.lazai.cn
www.dpzy.net.suidun.cn
www.hkpinjian.com.tujiachen.cn
www.yiwu-food.com.anhetong.cn
www.3m.com.cn.tihujiu.cn
www.lyfh.com.huatoutou.cn
www.yafangtissue.com.xiaolaige.cn
www.jinguan.com.cn.lvdate.cn
www.xmeastocean.com.soubianlu.cn
www.deyitex.com.fuenbu.cn
www.raiseeptc.com.zouyizou.cn
www.htjx819.com.dadudu.cn
www.tsbenwu.com.lazhuyong.cn
www.bjyutao.com.aizishu.cn
www.chemchi.com.wanyuecun.cn
www.ahmyhb.com.hetongmei.cn
www.btndhb.com.mofaya.cn
www.xadyz.com.ledatong.cn
www.tmfj.com.ebuyun.cn
www.water-ky.com.gayijiu.cn
www.lbtyyl.com.liqinge.cn
www.yonglibelt.com.hexiaolia.cn
www.jsqitai.com.yunyuewei.cn
www.comfast.cn.kemensen.cn
www.tceptech.com.ximaguohe.cn
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/70040412/viewspace-3084669/ ,如需转载,请注明出处,否则将追究法律责任。