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. 从 Android 源码到 apk 的编译打包流程

    文中涉及到的工具所在目录:Android/sdk/build-tools.下面开始分解并逐步实现对源码的打包. 编译流程 1. 生成仅包含资源文件的 apk 包和 R.java 文件 根据资源文件和 ...

  2. 在Linux中,当需要从磁盘读取块时,进程状态会发生什么变化?被封锁了吗?如果是这样,如何选择另一个流程来执行?

    当某个进程需要从磁盘中获取数据时,它实际上会停止在CPU上运行以让其他进程运行,因为该操作可能需要很长时间才能完成-至少需要5ms的磁盘寻道时间,而5ms就是1000万从程序的角度来看,CPU周期是永 ...

  3. shell常用分隔符及管道的用法

    1.命令1;命令2;命令3;.... 代码顺序执行 2.&&连接两条命令:命令1&&命令2&&命令3... 短路执行 3.||连接两条命令:命令1||命 ...

  4. Summer training #7

    B:读懂题意模拟 #include <bits/stdc++.h> #include <cstring> #include <iostream> #include ...

  5. 服务器syns to listen sockets drop导致创建socket失败

    在一次测试执行过程中,发现服务器TCP发送队列较长,执行netstat -s | grep LISTEN,发现有SYNs包被丢弃,但是没有times the listen queue of a soc ...

  6. ACID理解

    数据库事物的4个特性. A原子性:多次操作要么全部成功,要么全部失败.undo日志是在事务执行失败的时候撤销对数据库的操作,保证了事务的原子性(Atomicity) C一致性:一致性这个最不好理解.数 ...

  7. JAVA解压ZIP文件

    import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.Inp ...

  8. 经常使用的js三元表达式

    语法:条件?表达式1:表达式2 .  条件的括号可要可不要的 let istrue = true; let a = (istrue === true) ? '我是true啊' : "我是fa ...

  9. pdf缩略图生成上传解决方案

    前言:因自己负责的项目(jetty内嵌启动的SpringMvc)中需要实现文件上传,而自己对java文件上传这一块未接触过,且对 Http 协议较模糊,故这次采用渐进的方式来学习文件上传的原理与实践. ...

  10. SQL语句中 NOT IN 子句的“正确打开方式”

    在写SQL语句的时候,若where条件是判断用户不在某个集合当中,我们习惯使用 where 列名 not in (集合) 子句,这种写法本身没有问题,但实践过程中却发现很多人在写类似的SQL语句时,写 ...