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.
class Codec {
public: TreeNode * encode(Node* root) {
if (!root) return nullptr;
TreeNode* ret = new TreeNode(root->val);
TreeNode* tmp = ret;
if (root->children.size() != ) {
tmp->left = encode(root->children[]);
}
tmp = tmp->left;
for (int i = ; i < root->children.size(); i++) {
tmp->right = encode(root->children[i]);
tmp = tmp->right;
}
return ret;
}
Node* decode(TreeNode* root) {
if (!root) return nullptr;
Node* ret = new Node(root->val, vector<Node*>());
TreeNode*tmp = root->left;
while (tmp) {
ret->children.push_back(decode(tmp));
tmp = tmp->right;
}
return ret;
}
};

LC 431. Encode N-ary Tree to Binary Tree 【lock,hard】的更多相关文章

  1. LC 272. Closest Binary Search Tree Value II 【lock,hard】

    Given a non-empty binary search tree and a target value, find k values in the BST that are closest t ...

  2. LC 425. Word Squares 【lock,hard】

    Given a set of words (without duplicates), find all word squares you can build from them. A sequence ...

  3. LC 683. K Empty Slots 【lock,hard】

    There is a garden with N slots. In each slot, there is a flower. The N flowers will bloom one by one ...

  4. LC 727. Minimum Window Subsequence 【lock,hard】

    Given strings S and T, find the minimum (contiguous) substring W of S, so that T is a subsequenceof  ...

  5. LC 465. Optimal Account Balancing 【lock,hard】

    A group of friends went on holiday and sometimes lent each other money. For example, Alice paid for ...

  6. LC 774. Minimize Max Distance to Gas Station 【lock,hard】

    On a horizontal number line, we have gas stations at positions stations[0], stations[1], ..., statio ...

  7. LC 644. Maximum Average Subarray II 【lock,hard】

    Given an array consisting of n integers, find the contiguous subarray whose length is greater than o ...

  8. [LeetCode] Encode N-ary Tree to Binary Tree 将N叉树编码为二叉树

    Design an algorithm to encode an N-ary tree into a binary tree and decode the binary tree to get the ...

  9. LC 987. Vertical Order Traversal of a Binary Tree

    Given a binary tree, return the vertical order traversal of its nodes values. For each node at posit ...

随机推荐

  1. Samba编码设置方法

    弟管理學校的網頁伺服器,該伺服器也同時是大家的分享檔案集散中心,是以Linux架設起來的,該伺服器以 Unicode 作為系統編碼,而其他Windows系統則是big5(MS950)編碼,最近我要讓另 ...

  2. Tensorflow模型代码调试问题

    背景: 不知道大家有没有这样的烦恼:在使用Tensorflow搭建好模型调试的过程中,经常会碰到一些问题,当时花了不少时间把这个问题解决了,一段时间后,又出现了同样的问题,却怎么也不记得之前是怎么解决 ...

  3. linux下安装压缩解压程序7z命令及7z命令的使用

    1.1 在线安装如果你的宿主机Linux可以连接外网,推荐用这种方式,方便简单,执行命令:sudo apt-get install p7zip即可在线安装7z命令. 1.2 安装包安装7z(准确点说是 ...

  4. 分布式 一致性Paxos算法(转载)

    比较通俗易懂,可以入门,转载地址是http://www.cnblogs.com/linbingdong/p/6253479.html Paxos算法在分布式领域具有非常重要的地位.但是Paxos算法有 ...

  5. input和textarea的区别

    区别: <textarea>标签是成对的,有结束标签进行闭合,标签的内容写在标签对中间:<input>是单个标签,标签的内容通过 value 属性设置: <textare ...

  6. puppet完全攻略(二)让puppet代码支持vim高亮显示

    puppet完全攻略(二)让puppet代码支持vim高亮显示 2012-06-10 13:33:01 标签:puppet viong puppet完全攻略 原创作品,允许转载,转载时请务必以超链接形 ...

  7. 【原】maven web项目eclipse搭建

    1.new->other->Maven Project,点击next 2.继续next 3.选择maven-archetype-webapp,如果找不到可以再Filter里面搜索web,就 ...

  8. js+分布上传大文件

    文件夹上传:从前端到后端 文件上传是 Web 开发肯定会碰到的问题,而文件夹上传则更加难缠.网上关于文件夹上传的资料多集中在前端,缺少对于后端的关注,然后讲某个后端框架文件上传的文章又不会涉及文件夹. ...

  9. webpack官方文档分析(二):概念

    1.概念 webpack的核心是将JavaScript应用程序的静态捆绑模块.当webpack处理您的应用程序时,它会在内部构建一个依赖关系图,它映射您的项目所需的每个模块并生成一个或多个包. 从版本 ...

  10. mysql时区配置

    1.修改linux系统时区:ln -sf /usr/share/zoneinfo/America/Los_Angeles /etc/localtime 修改为美洲美国洛杉矶时间 2.查看mysql时区 ...