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. IPC之sem.c源码解读

    // SPDX-License-Identifier: GPL-2.0 /* * linux/ipc/sem.c * Copyright (C) 1992 Krishna Balasubramania ...

  2. LeetCode3.无重复字符的最大子串

    给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 示例 1: 输入: "abcabcbb"输出: 3 解释: 因为无重复字符的最长子串是 "abc&quo ...

  3. 【vuex】mutation和action的区别

    const store = new Vuex.Store({ state: { count: 0 }, mutations: { increment (state) { state.count++ } ...

  4. Android异常与性能优化相关面试问题-冷启动优化面试问题详解

    什么是冷启动: 冷启动的定义:冷启动就是在启动应用前,系统中没有该应用的任何进程信息.实际也就是要执行Application.onCreate()方法的那次启动. 冷启动 / 热启动的区别:热启动:用 ...

  5. phpStudy环境下composer的安装

    前言 原来是做php开发的,现在转行前端工程师,因为很久没有接触了,可能会有其他问题,这里简单记录一下,后期遇到什么问题再进行更新~ 话说下载特别慢所以这里给个网盘链接Composer-Setup.e ...

  6. js特效 15个小demo

    js特效和15个小demo 代码如下:images文件夹未上传 1.图片切换: <!DOCTYPE html> <html> <head> <title> ...

  7. vscode安装eslint插件,代码统一自动修复

    ESlint:是用来统一JavaScript代码风格的工具,不包含css.html等. 方法和步骤: 通常情况下vue项目都会添加eslint组件,我们可以查看webpack的配置文件package. ...

  8. 将页面内搜索结果高亮显示(使用mark.js),且页面顶部定位到第一个搜索结果

    <!DOCTYPE> <html> <head> <title> new document </title> <meta name=& ...

  9. CSS选择器div和p的用法和区别

    div,p.div p.div>p.div+p.div~p.div.a的用法和区别 div,p:选择所有<div>元素和<p>元素 <style> p,spa ...

  10. Python3:Collatz 序列(考拉咨猜想)

    编写一个名为collatz()的函数,它有一个名为number的参数: 如果参数是偶数,那么collatz()就打印出number//2,并返回该值: 如果number是奇数,那么collatz()就 ...