当前位置:网站首页>【Leetcode】431. Encode N-ary Tree to Binary Tree(困难)
【Leetcode】431. Encode N-ary Tree to Binary Tree(困难)
2022-06-23 04:31:00 【明朗晨光】
一、题目
1、题目描述
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:
Input: root = [1,null,3,2,4,null,5,6]
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.
2、基础框架
public class EncodeNaryTreeToBinaryTree {
public static class Node {
public int val;
public List<Node> children;
public Node() {
}
public Node(int _val) {
val = _val;
}
public Node(int _val, List<Node> _children) {
val = _val;
children = _children;
}
};
public static class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}
class Codec {
// Encodes an n-ary tree to a binary tree.
public TreeNode encode(Node root) {
}
// Decodes your binary tree to an n-ary tree.
public Node decode(TreeNode root) {
}
}
}
3、原题链接
二、解题报告
1、思路分析
【题意理解】就是将多叉树转换成二叉树,用二叉树来序列化多叉树。
【思路】将多叉树上 X 节点的所有子孩子放在 X 节点的左树的右边界上,只要能将多叉树转成一种无歧义的二叉树结构就可以。
2、时间复杂度
O ( N ) O(N) O(N)
3、代码详解
public class EncodeNaryTreeToBinaryTree {
public static class Node {
public int val;
public List<Node> children;
public Node() {
}
public Node(int _val) {
val = _val;
}
public Node(int _val, List<Node> _children) {
val = _val;
children = _children;
}
};
public static class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}
class Codec {
// Encodes an n-ary tree to a binary tree.
public TreeNode encode(Node root) {
if (root == null) {
return null;
}
TreeNode head = new TreeNode(root.val);
head.left = en(root.children);
return head;
}
private TreeNode en(List<Node> children) {
TreeNode head = null;
TreeNode cur = null;
for (Node child : children) {
//遍历孩子节点
TreeNode tNode = new TreeNode(child.val);
if (head == null) {
//只有第一个孩子节点会进入这个if条件
head = tNode;
} else {
cur.right = tNode;
}
cur = tNode;
cur.left = en(child.children); //深度优先遍历,当前子节点处理完毕之后再处理下一个孩子
}
return head;
}
// Decodes your binary tree to an n-ary tree.
public Node decode(TreeNode root) {
if (root == null) {
return null;
}
return new Node(root.val, de(root.left));
}
public List<Node> de(TreeNode root) {
List<Node> children = new ArrayList<>();
while (root != null) {
Node cur = new Node(root.val, de(root.left)); //用深度优先的方法反序列化
children.add(cur);
root = root.right;
}
return children;
}
}
}
边栏推荐
- 工作积累-判断GPS是否打开
- PAT 乙等 1018 C语言
- Huawei's software and hardware ecosystem has taken shape, fundamentally changing the leading position of the United States in the software and hardware system
- jvm-04. Object's memory layout
- [image fusion] sparse regularization based on non convex penalty to realize image fusion with matlab code
- Memory analysis and memory leak detection
- ant使用总结(三):批量打包apk
- Leetcode topic resolution divide two integers
- [database backup] complete the backup of MySQL database through scheduled tasks
- ant使用总结(二):相关命令说明
猜你喜欢

Data migration from dolphin scheduler 1.2.1 to dolphin scheduler 2.0.5 and data test records after migration

True MySQL interview question (XXII) -- condition screening and grouping screening after table connection

Visual studio debugging tips

Kotlin Android simple activity jump, simple combination of handler and thread

jvm-04.对象的内存布局

Addressing and addressing units

The digital collection market has just begun

Radar canvas

True MySQL interview question (24) -- row column exchange

Centos7部署radius服务-freeradius-3.0.13-15.el7集成mysql
随机推荐
Dolphin scheduler dolphin scheduling upgrade code transformation -upgradedolphin scheduler
Software design and Development Notes 2: serial port debugging tool based on QT design
vant weapp日历组件性能优化 Calendar 日历添加min-date最小日期页面加载缓慢
Pat class B 1022 d-ary a+b
Fraction to recursing decimal
PAT 乙等 1016 C语言
App SHA1 acquisition program Baidu map Gaode map simple program for acquiring SHA1 value
Infotnews | which Postcard will you receive from the universe?
PAT 乙等 1010 C语言
PAT 乙等 1013 C语言
Pat class B 1009 C language
The 510000 prize pool invites you to participate in the competition -- the second Alibaba cloud ECS cloudbuild developer competition is coming
Activity启动模式和生命周期实测结果
ORB_ Slam2 operation
线性表 链表结构的实现
Add and multiply two polynomials using linked list
True MySQL interview question (21) - Finance - overdue loan
Centos7部署radius服务-freeradius-3.0.13-15.el7集成mysql
Pat class B 1021 digit statistics
Kotlin android简单Activity跳转、handler和thread简单配合使用