当前位置:网站首页>431. 将 N 叉树编码为二叉树 DFS
431. 将 N 叉树编码为二叉树 DFS
2022-07-01 03:27:00 【钰娘娘】
431. 将 N 叉树编码为二叉树
设计一个算法,可以将 N 叉树编码为二叉树,并能将该二叉树解码为原 N 叉树。一个 N 叉树是指每个节点都有不超过 N 个孩子节点的有根树。类似地,一个二叉树是指每个节点都有不超过 2 个孩子节点的有根树。你的编码 / 解码的算法的实现没有限制,你只需要保证一个 N 叉树可以编码为二叉树且该二叉树可以解码回原始 N 叉树即可。
例如,你可以将下面的
3-叉树以该种方式编码:
注意,上面的方法仅仅是一个例子,可能可行也可能不可行。你没有必要遵循这种形式转化,你可以自己创造和实现不同的方法。
注意:
N的范围在[1, 1000]- 不要使用类成员 / 全局变量 / 静态变量来存储状态。你的编码和解码算法应是无状态的。
做题结果
成功,这题难的是构思,构思出来了就很简单
方法:DFS
引用评论区一堆人说的话“左孩子,右兄弟”,补个图。
对于左侧多叉数,第一层是 [1],第二层[2,3,4]。那我们把第二层的第一个元素作为本层[1],二叉树的左子树,然后 3,4 节点作为和 2 同层的节点,放在 2 的右子树。

多叉树到二叉树:
1. 现在是根,定义子树前驱节点
2-1. 构建子树,子树有前驱节点,则当前节点是前驱节点的兄弟节点,前驱节点的右指针指向当前元素
2-2. 构建子树,子树没有前驱节点,则说明是本层的第一个元素,父节点的左指针指向它
3. 修改前驱节点
二叉树到多叉树:
1. 现在是根,检查左子树是否为空,非空时构建子树
2. 创建子树并添加到当前的子节点中,不断向二叉树的右子树移动,构造多叉树同层节点
class Codec {
// Encodes an n-ary tree to a binary tree.
public TreeNode encode(Node root) {
if(root == null) return null;
TreeNode t = new TreeNode(root.val);
TreeNode pre = null;
//子树放左边,同层子树一直放置为前一个的右子树
if(root.children!=null){
for(Node v:root.children){
TreeNode inner = encode(v);
if(pre!=null){
pre.right = inner;
}else{
t.left = inner;
}
pre = inner;
}
}
return t;
}
// Decodes your binary tree to an n-ary tree.
public Node decode(TreeNode root) {
if(root == null) return null;
Node n = new Node(root.val);
n.children = new ArrayList<>();
if(root.left!=null){
TreeNode curr = root.left;
while (curr!=null){
Node v = decode(curr);
n.children.add(v);
curr = curr.right;
}
}
return n;
}
}边栏推荐
- 在线公网安备案保姆级教程【伸手党福利】
- How to display scrollbars on the right side of the background system and how to solve the problem of double scrollbars
- 【伸手党福利】JSONObject转String保留空字段
- Asgnet paper and code interpretation 2
- Cookie&Session
- 详解Spark运行模式(local+standalone+yarn)
- 衡量两个向量相似度的方法:余弦相似度、pytorch 求余弦相似度:torch.nn.CosineSimilarity(dim=1, eps=1e-08)
- 168. excel table column name
- Promql select time series
- 【EI会议】2022年国际土木与海洋工程联合会议(JCCME 2022)
猜你喜欢

5、【WebGIS实战】软件操作篇——服务发布及权限管理

LeetCode 31下一个排列、LeetCode 64最小路径和、LeetCode 62不同路径、LeetCode 78子集、LeetCode 33搜索旋转排序数组(修改二分法)

RSN:Learning to Exploit Long-term Relational Dependencies in Knowledge Graphs

谷粒学院微信扫码登录过程记录以及bug解决

Ultimate dolls 2.0 | encapsulation of cloud native delivery

【TA-霜狼_may-《百人计划》】2.4 传统经验光照模型

Jeecgboot output log, how to use @slf4j

pytorch nn. AdaptiveAvgPool2d(1)

Home online shopping project

[TA frost wolf \u may - "hundred people plan"] 2.1 color space
随机推荐
Cookie&Session
FCN full Convolution Network Understanding and Code Implementation (from pytorch Official Implementation)
Edge drawing: a combined real-time edge and segment detector
【TA-霜狼_may-《百人計劃》】2.3 常用函數介紹
TEC: Knowledge Graph Embedding with Triple Context
Binary tree god level traversal: Morris traversal
pytorch中的双线性插值上采样(Bilinear Upsampling)、F.upsample_bilinear
Blueprism registration, download and install -rpa Chapter 1
torch.histc
241. Design priorities for operational expressions
Pytorch training deep learning network settings CUDA specified GPU visible
LeetCode 144二叉树的前序遍历、LeetCode 114二叉树展开为链表
5、【WebGIS实战】软件操作篇——服务发布及权限管理
静态库使用MFC和共享库使用MFC的区别
pytorch nn.AdaptiveAvgPool2d(1)
8. string conversion integer (ATOI)
[reach out to Party welfare] developer reload system sequence
392. 判断子序列
Take you through a circuit board, from design to production (dry goods)
You cannot right-click F12 to view the source code solution on the web page
