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 ...
随机推荐
- Shell脚本变量与判断
变量 环境变量 局部变量 declare 定义变量类型 本地变量 local 在函数中使用 变量类型: 数值型: 1.整形 2.浮点型 3.布尔型 0 1 (真 假) (true false) 字符串 ...
- Java的概述
Java的基本概述 Java是SUN(Stanford University Network),斯坦福大学网络公司)1995年推出的一门高级编程语言.Java是一种面向Internet的编程语言.随着 ...
- 如何使用ssh工具便于远程管理
前几天亲眼经历了Linux服务器运维过程,眼看着别人熟练运用Linux管理工具,自个心里不是滋味,所以自己特意整理了一篇“专题”:使用ssh服务远程管理主机. 首先在使用ssh服务工具之前,先熟悉下L ...
- linux 启动tomcat
操作步骤: 第一步:进入tomcat的bin目录 cd /usr/local/tomcat/bin 第二步:使用tomcat关闭命令 ./shutdown.sh 第三步:查看tomcat是否关闭 ps ...
- MySQL之concat、concat_ws、group_concat
concat(str1, str2, ...) 返回结果为连接一起的字符串. concat_ws(separator, str1, str2, ...) 同concat,但是可以指定连接符,sepa ...
- CentOS7.x卸载与安装MySQL5.7的操作过程以及编码格式的修改
一.MySQL5.7的卸载 1.1yum方式查看yum是否安装过mysql cd yum list installed mysql* 如或显示了列表,说明系统中有MySQL 如上显示,我已经安装了my ...
- 卸载CUDA和cuDNN
卸载CUDA和cuDNN 1.卸载CUDA 本教程只针对对于.run方式安装的,其他的没有进行测试 打开终端,输入sudo /usr/local/cuda-10.0/bin/uninstall_cud ...
- 微信获取用户列表的json字符串解析
今天学习微信遇到一个json的解析,但是因为自己的模型思维和思考能力很差一直困扰最后经过询问解决的问题,其实问题很简单总结起来就是json的解析: 注释:今天主要是讲怎样解析json的所以其他方法就只 ...
- C#形参和实参、引用类型和值类型使用时的一个注意点。
这是早上群里讨论的例子. static void main(string [] arg){ var p1=new Person{Name="张三"}; var p2=new Per ...
- jQuery数据管理:Kendo UI过滤器设置运算符
Kendo UI for jQuery最新试用版下载 Kendo UI目前最新提供Kendo UI for jQuery.Kendo UI for Angular.Kendo UI Support f ...