LC 431. Encode N-ary Tree to Binary Tree 【lock,hard】
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:
Nis in the range of[1, 1000]- 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】的更多相关文章
- 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 ...
- LC 425. Word Squares 【lock,hard】
Given a set of words (without duplicates), find all word squares you can build from them. A sequence ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- [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 ...
- 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 ...
随机推荐
- KMS激活的密钥
今天办公电脑黑屏了,仔细一看,变成了未激活.于是从网上找到了所有Windows版本的GVLK密钥,试了一下完美激活,应该是180天的吧.先用着,等下次过期了再说,哈哈. GVLK密钥是专门用于KMS激 ...
- 怎么处理系统蓝屏后提示代码0x000000d1的错误?
电脑开机有时会出现蓝屏,导致蓝屏的原因有很多,每种错误都有不同的代码.下面就来和大家分享一下电脑开机蓝屏出现0x000000d1错误代码是什么原因?我们又该怎么去解决这个问题. 电脑开机蓝屏出现0x0 ...
- 理解函数声明--signal函数的声明
1.显示调用首地址为0的例程:(*(void(*)())0)() 显示调用首地址为0的例程的表达式为:(*(void(*)())0)() 分两步分析: 假定变量fp是一个函数指针,调用方法如下:(*f ...
- 论文笔记:Unsupervised Domain Adaptation by Backpropagation
14年9月份挂出来的文章,基本思想就是用对抗训练的方法来学习domain invariant的特征表示.方法也很只管,在网络的某一层特征之后接一个判别网络,负责预测特征所属的domain,而后特征提取 ...
- 使用 jenkins 为 nginx 增加上下文
每次需要在Nginx增加上下文,都需要添加如下两段 server.conf upstream serverdownloadPage { server 10.11.19.6:3023; } ht ...
- JAVA WEB之Servlet使用
3. JSP提交数据和Servlet程序设计 想要将JSP数据提交,主要的方法有form表单方式.url方式和Session方式.将JSP数据传递给后台,form表单显然比较简单方便. 正如上一节中展 ...
- es实战之数据导出成csv文件
从es将数据导出分两步: 查询大量数据 将数据生成文件并下载 本篇主要是将第二步,第一步在<es实战之查询大量数据>中已讲述. csv vs excel excel2003不能超过6553 ...
- VUE-文本-事件-属性指令
一.Vue文本指令 文本指令: 1.{{ }} 2.v-text:不能解析html语法的文本,会原样输出 3.v-html:能解析html语法的文本 4.v-once:处理的标签的内容只能被解析一次 ...
- router-link to 动态赋值
路由定义: 动态赋值: <router-link :to="{path:'/old_data_details/params/'+item.id}" > </rou ...
- js swal()弹出框
做前端开发的时候时常会遇到修改成功.新增成功这类弹出框,用alert的话未免有点太low了,而swal()是一个简单又实用的弹出框方法 alert 弹出框样式如下: swal() 弹出框样式如下: 代 ...