Design an algorithm to encode an N-ary tree into a binary tree and decode the binary tree to get the original N-ary tree. An N-ary tree is a rooted tree in which each node has no more than N children. Similarly, a binary tree is a rooted tree in which each node has no more than 2 children. There is no restriction on how your encode/decode algorithm should work. You just need to ensure that an N-ary tree can be encoded to a binary tree and this binary tree can be decoded to the original N-nary tree structure.

For example, you may encode the following 3-ary tree to a binary tree in this way:

Note that the above is just an example which might or might not work. You do not necessarily need to follow this format, so please be creative and come up with different approaches yourself.

Note:

  1. N is in the range of [1, 1000]
  2. Do not use class member/global/static variables to store states. Your encode and decode algorithms should be stateless.

这道题让我们将一棵N叉树编码为二叉树,其实还需要将二叉树解码回N叉树。题目中说了具体的编解码的方法无所谓,那么就怎么简单怎么来呗。首先想一下这道题的难点是什么,N叉树的特点的每个结点最多有N个子结点,而二叉树的每个结点最多只能有两个子结点,那么多余的子结点怎么办,当然只能继续子结点下继续累加,就像泡泡龙游戏一样,一个挂一个的。如何累,得确定一套具体的规则,这样解码的时候,反向来一遍就可以了。对于当前结点 root 的N个子结点的处理办法是,将第一个结点挂到二叉树的左子结点上,然后将后面的结点依次挂到右子结点,和右子结点的右子结点上,这样做的好处是,同一层的子结点都挂在右子结点上,而这些子结点自己的子结点都会挂在左子结点上,听起来很晕是么,那就举例说明一下吧,就用题目中的例子中的树好了(注意题目中说只要能把N叉树编码成二叉树,然后再解码回原N叉树,并不 care 到底编码成啥样的二叉树)。

N-ary Tree:

   /  |  \

 / \

Binary Tree:

   /

 / \

 \   \
     

我们可以看出,N叉树根结点1的第一个子结点3被挂到了二叉树的左子结点上,同一层的结点2挂到了结点3的右子结点上,同一层的结点4被挂到了结点2的右子结点上。而结点3本身的子结点也按照这个规律,第一个子结点5挂到了结点3的左子结点上,而同一排的结点6挂到了结点5的右子结点上。

对于解码,也是同样的规律,先根据根结点值新建一个空的N叉树结点,由于我们的编码规律,根结点是一定没有右子结点的,所以取出左子结点 cur,并且开始循环,如果 cur 结点存在,那么我们对 cur 递归调用解码函数,将返回的结点加入当前N叉树结点的 children 数组中,然后 cur 再赋值为其右子结点,继续递归调用解码函数,再加入 children 数组,如此便可将二叉树还原为之前的N叉树,参见代码如下:

class Codec {
public: // Encodes an n-ary tree to a binary tree.
TreeNode* encode(Node* root) {
if (!root) return NULL;
TreeNode *res = new TreeNode(root->val);
if (!root->children.empty()) {
res->left = encode(root->children[]);
}
TreeNode *cur = res->left;
for (int i = ; i < root->children.size(); ++i) {
cur->right = encode(root->children[i]);
cur = cur->right;
}
return res;
} // Decodes your binary tree to an n-ary tree.
Node* decode(TreeNode* root) {
if (!root) return NULL;
Node *res = new Node(root->val, {});
TreeNode *cur = root->left;
while (cur) {
res->children.push_back(decode(cur));
cur = cur->right;
}
return res;
}
};

类似题目:

Serialize and Deserialize N-ary Tree

参考资料:

https://leetcode.com/problems/encode-n-ary-tree-to-binary-tree/

https://leetcode.com/problems/encode-n-ary-tree-to-binary-tree/discuss/153061/Java-Solution-(Next-Level-greater-left-Same-Level-greater-right)

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] Encode N-ary Tree to Binary Tree 将N叉树编码为二叉树的更多相关文章

  1. 【一天一道LeetCode】#104. Maximum Depth of Binary Tree

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源:http ...

  2. 【LeetCode】297. Serialize and Deserialize Binary Tree 解题报告(Python)

    [LeetCode]297. Serialize and Deserialize Binary Tree 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode ...

  3. 【LeetCode】662. Maximum Width of Binary Tree 解题报告(Python)

    [LeetCode]662. Maximum Width of Binary Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.co ...

  4. 33. Minimum Depth of Binary Tree && Balanced Binary Tree && Maximum Depth of Binary Tree

    Minimum Depth of Binary Tree OJ: https://oj.leetcode.com/problems/minimum-depth-of-binary-tree/ Give ...

  5. [LeetCode] Verify Preorder Serialization of a Binary Tree 验证二叉树的先序序列化

    One way to serialize a binary tree is to use pre-oder traversal. When we encounter a non-null node, ...

  6. [LeetCode] Lowest Common Ancestor of a Binary Tree 二叉树的最小共同父节点

    Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According ...

  7. LeetCode之104. Maximum Depth of Binary Tree

    -------------------------------- 递归遍历即可 AC代码: /** * Definition for a binary tree node. * public clas ...

  8. 【LeetCode OJ】Maximum Depth of Binary Tree

    Problem Link: https://oj.leetcode.com/problems/maximum-depth-of-binary-tree/ Simply BFS from root an ...

  9. LeetCode Verify Preorder Serialization of a Binary Tree

    原题链接在这里:https://leetcode.com/problems/verify-preorder-serialization-of-a-binary-tree/ 题目: One way to ...

随机推荐

  1. line-height && vertical-align 学习总结

    前言 line-height.font-size.vertical-align是设置行内元素布局的关键属性.这三个属性是相互依赖的关系,改变行间距离.设置垂直对齐等都需要它们的通力合作. 行高 lin ...

  2. EffectiveC++ 第2章 构造/析构/赋值运算

    我根据自己的理解,对原文的精华部分进行了提炼,并在一些难以理解的地方加上了自己的"可能比较准确"的「翻译」. Chapter 2 构造 / 析构 / 赋值 条款 05:了解C++ ...

  3. windows服务器基本管理及服务搭建

    windows服务器基本管理及服务搭建 ****windows服务器系统版本:2000 2003 2008 2012 1.用户与组管理 用户:账户=账号/用户名+密码 每个账户有自己唯一的SID 账户 ...

  4. webpack学习笔记——sourcemap(使用webpack打包的项目如何调试代码)

    [webpack]devtool里的7种SourceMap模式是什么鬼? 里面详细介绍了7种模式的区别,和建议使用. webpack sourcemap 选项多种模式的一些解释 两篇文章大同小异,第一 ...

  5. webpack安装异常

    webpack中文指南:https://zhaoda.net/webpack-handbook/index.html 今天中午,我用   cnpm install   重新下载了一下项目的依赖,爆了一 ...

  6. python 读写文件中 w与wt ; r与rt 的区别

    w,r,wt,rt都是python里面文件操作的模式.w是写模式,r是读模式.t是windows平台特有的所谓text mode(文本模式),区别在于会自动识别windows平台的换行符.类Unix平 ...

  7. WEB 3D SVG CAD 向量 几个实施(转)

      一.他们所有的发展.从地上爬起来 VML+SVG发展矢量地图.你并不需要导入第三方的图片作为背景,直接在地图编辑器可以在底图内容编辑,由于岩石.巷道.煤层.画水.础地图样子再在其上面画出智慧线等设 ...

  8. 我实在不懂Python的Asyncio

    原语 事件循环(Event Loop) Awaitables和Coroutines Coroutine Wrappers Awaitables and Futures Tasks Handles Ex ...

  9. 023_supervisorctl管理服务注意事项

    一. (1)问题 我在线上使用supervisorctl管理服务时遇到程序文件更新了,但是下次supervisorctl执行的时候并没有更新, 原因是supervisor更新后必须重新读取加载文件才行 ...

  10. 【原创】Linux基础之sudo

    sudo允许用户以其他用户的身份(比如root)执行命令,比如切换用户.执行命令.读写文件等: 配置 sudo配置在:/etc/sudoers ## Sudoers allows particular ...