文章目录

  • 1. 题目
  • 2. 解题
    • 2.1 递归
    • 2.2 BFS

1. 题目

设计一个算法,可以将 N 叉树编码为二叉树,并能将该二叉树解码为原 N 叉树。
一个 N 叉树是指每个节点都有不超过 N 个孩子节点的有根树。
类似地,一个二叉树是指每个节点都有不超过 2 个孩子节点的有根树。
你的编码 / 解码的算法的实现没有限制,你只需要保证一个 N 叉树可以编码为二叉树且该二叉树可以解码回原始 N 叉树即可。

例如,你可以将下面的 3-叉 树以该种方式编码:

注意,上面的方法仅仅是一个例子,可能可行也可能不可行。
你没有必要遵循这种形式转化,你可以自己创造和实现不同的方法。

注意:
N 的范围在 [1, 1000]
不要使用类成员 / 全局变量 / 静态变量来存储状态。
你的编码和解码算法应是无状态的。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/encode-n-ary-tree-to-binary-tree
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

2. 解题

  • 参考官方思路,第一个子节点2接到父节点1的left,其余的兄弟节点 3,4 都接在其左边兄弟节点的right

2.1 递归

/*
// Definition for a Node.
class Node {
public:int val;vector<Node*> children;Node() {}Node(int _val) {val = _val;}Node(int _val, vector<Node*> _children) {val = _val;children = _children;}
};
*//*** Definition for a binary tree node.* struct TreeNode {*     int val;*     TreeNode *left;*     TreeNode *right;*     TreeNode(int x) : val(x), left(NULL), right(NULL) {}* };*/class Codec {public:// Encodes an n-ary tree to a binary tree.TreeNode* encode(Node* root) {if(!root) return NULL;TreeNode* newroot = new TreeNode(root->val);TreeNode* cur = NULL;if(!root->children.empty()){newroot->left = encode(root->children[0]);cur = newroot->left;}for(int i = 1; i < root->children.size(); ++i){cur->right = encode(root->children[i]);cur = cur->right;}return newroot;}// Decodes your binary tree to an n-ary tree.Node* decode(TreeNode* root) {if(!root) return NULL;Node *newroot = new Node(root->val);TreeNode *cur = NULL;if(root->left){newroot->children.push_back(decode(root->left));cur = root->left;}while(cur && cur->right){newroot->children.push_back(decode(cur->right));cur = cur->right;}return newroot;}
};

108 ms 179.4 MB

2.2 BFS

class Codec {public:// Encodes an n-ary tree to a binary tree.TreeNode* encode(Node* root) {if(!root) return NULL;TreeNode* newroot = new TreeNode(root->val), *newTreeNode = NULL;TreeNode* cur = NULL;queue<pair<Node*, TreeNode*>> q;q.push({root,newroot});while(!q.empty()){int size = q.size();while(size--){root = q.front().first;newTreeNode = q.front().second;q.pop();if(!root->children.empty()){newTreeNode->left = new TreeNode(root->children[0]->val);cur = newTreeNode->left;q.push({root->children[0], cur});}for(int i = 1; i < root->children.size(); ++i){cur->right = new TreeNode(root->children[i]->val);;cur = cur->right;q.push({root->children[i], cur});}}}return newroot;}// Decodes your binary tree to an n-ary tree.Node* decode(TreeNode* root) {if(!root) return NULL;Node *newroot = new Node(root->val), *newNode = NULL;Node *cur = NULL;queue<pair<TreeNode*, Node*>> q;q.push({root,newroot});while(!q.empty()){int size = q.size();while(size--){root = q.front().first;cur = q.front().second;q.pop();if(root->left){newNode = new Node(root->left->val);cur->children.push_back(newNode);q.push({root->left, newNode});root = root->left;while(root->right){newNode = new Node(root->right->val);cur->children.push_back(newNode);q.push({root->right, newNode});root = root->right;}}}}return newroot;}
};

80 ms 173.6 MB


我的CSDN博客地址 https://michael.blog.csdn.net/

长按或扫码关注我的公众号(Michael阿明),一起加油、一起学习进步!

LeetCode 431. 将 N 叉树编码为二叉树(递归/层序)相关推荐

  1. Leetcode431.将N叉树编码为二叉树(golang)

    思路简述 一道困难题,是一道偏设计类的题目,首先第一个困难点就是如何用二叉树来表示一颗N叉树,其中一种可行常见的思想是:二叉树中节点的左子树为原N叉树中的孩子,右子树为原N叉树中的兄弟节点,下面是一个 ...

  2. LeetCode 102二叉树的层序遍历103二叉树锯齿形遍历104二叉树的最大深度

    微信搜一搜:bigsai 大家都在关注的刷题.学习数据结构和算法宝藏项目 关注回复进群即可加入力扣打卡群,欢迎划水.近期打卡: LeetCode 97交错字符串(动态规划) LeetCode 98验证 ...

  3. LeetCode 1490. 克隆 N 叉树(DFS/BFS)

    文章目录 1. 题目 2. 解题 2.1 DFS 2.2 BFS 1. 题目 给定一棵 N 叉树的根节点 root ,返回该树的深拷贝(克隆). N 叉树的每个节点都包含一个值( int )和子节点的 ...

  4. 【LeetCode】【HOT】617. 合并二叉树(递归)

    [LeetCode][HOT]617. 合并二叉树 文章目录 [LeetCode][HOT]617. 合并二叉树 package hot;import java.util.ArrayDeque; im ...

  5. 【LeetCode】【HOT】226. 翻转二叉树(递归)

    [LeetCode][HOT]226. 翻转二叉树 文章目录 [LeetCode][HOT]226. 翻转二叉树 package hot;import java.util.ArrayDeque; im ...

  6. 【LeetCode】【HOT】102. 二叉树的层序遍历(队列)

    [LeetCode][HOT]102. 二叉树的层序遍历 文章目录 [LeetCode][HOT]102. 二叉树的层序遍历 package hot;import java.util.ArrayLis ...

  7. 【LeetCode】【HOT】101. 对称二叉树(BFS+队列/递归)

    [LeetCode][HOT]101. 对称二叉树 文章目录 [LeetCode][HOT]101. 对称二叉树 package hot;class TreeNode{int val;TreeNode ...

  8. 【LeetCode】剑指 Offer 37. 序列化二叉树

    [LeetCode]剑指 Offer 37. 序列化二叉树 文章目录 [LeetCode]剑指 Offer 37. 序列化二叉树 package offer;import java.util.Link ...

  9. 【LeetCode】剑指 Offer 07. 重建二叉树

    [LeetCode]剑指 Offer 07. 重建二叉树 文章目录 [LeetCode]剑指 Offer 07. 重建二叉树 package offer;import java.util.ArrayD ...

最新文章

  1. 怎样在两小时内搞定 OpenStack 部署?
  2. 如何快速而准确的获取生物体的遗传信息一直是生命科学 中的一个非常重要的研究点
  3. 秒杀苹果carplay baidu车联网API冷艳北京车展
  4. hdu2435最大流最小割
  5. Linux下的设备大体分为三类,微机原理知识点总结
  6. 上如何刻字_校园石阶上被人刻了1700多个字?!这次网友却说好
  7. 查询七天之内所有生日的客户
  8. C语言程序设计第三节课作业
  9. centos 调整home分区xfs_centos下扩容根分区(针对xfs和ext4不同文件系统)
  10. @程序员,2019 年软件开发新趋势必知!
  11. php读写明华读卡器,rwCard 明华USBIC卡读卡器IC卡读写程序 - 下载 - 搜珍网
  12. [转]多CLIENT的SCC4设置
  13. TCP 拥塞控制详解
  14. sit是什么环境_DEV SIT UAT PET SIM PRD PROD常见环境英文缩写含义
  15. python请输入一个三位数输出该三位数的逆序数_编写程序,从键盘输入一个三位数,求出其逆序数并输出,例如输入123,输出321。...
  16. linux mt命令,几种操作系统mt命令比较
  17. PS混合模式图文详解
  18. 电脑设备管理器在哪里可以找到
  19. 中国现代书画家——谭奇中、李义象、高俊鹏等
  20. V2V-5G自动驾驶交流微信群

热门文章

  1. 脚本启动显示查询频繁被服务器防御_面对CC攻击,该如何进行防御
  2. 快速入门Matplotlib
  3. java arraylist string_在Java ArrayList String中使用contains
  4. 中班机器人上课视频_家委会:出班费买智能扫地机器人,不用家长搞卫生了,莫名其妙...
  5. 【原】webpack--loaders,主要解释为什么需要loaders和注意事项
  6. MySQL源码编译与初始化
  7. LA 3942 Remember the Word
  8. Spiral Matrix I II
  9. SQL SERVER PIVOT 行转列、列传行
  10. 挖掘有价值的搜索关键词