当前位置:网站首页>【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;
}
}
}
边栏推荐
- Heimdall database proxy scale out 20 times
- The author believes that the so-called industrial Internet is a process of deep integration of industry and the Internet
- jvm-05. garbage collection
- Pat class B 1026 program running time
- Pat class B 1023 minimum decimals
- APP SHA1获取程序 百度地图 高德地图获取SHA1值的简单程序
- How does win11 enable mobile hotspot? How to enable mobile hotspot in win11
- Advanced Mathematics (Seventh Edition) Tongji University exercises 1-9 personal solutions
- Radar canvas
- Oracle exception
猜你喜欢

Ant Usage Summary (II): description of related commands

android Handler内存泄露 kotlin内存泄露处理

iNFTnews | 加密之家从宇宙寄来的明信片,你会收到哪一张?

金融科技之高效办公(一):自动生成信托计划说明书

Digital collections - new investment opportunities

jvm-06.垃圾回收器

SSM project construction

编址和编址单位

Summary of ant usage (I): using ant to automatically package apk

runc 符号链接挂载与容器逃逸漏洞预警(CVE-2021-30465)
随机推荐
Pat class B 1023 minimum decimals
Pat class B 1014 C language
Advanced Mathematics (Seventh Edition) Tongji University exercises 1-7 personal solutions
APP SHA1获取程序 百度地图 高德地图获取SHA1值的简单程序
Leetcode topic analysis: factorial training zeroes
PAT 乙等 1022 D进制的A+B
Pit filling for abandoned openssl-1.0.2 (.A to.So)
Dolphin scheduler dolphin scheduling upgrade code transformation -upgradedolphin scheduler
How to specify the output path of pig register Project Log
Pyinstaller sklearn报错的问题
Heimdall database proxy scale out 20 times
gplearn出现 assignment destination is read-only
jvm-06. Garbage collector
Pat class B 1018 C language
New classes are launched | 5 minutes each time, you can easily play with Alibaba cloud container service!
Visual studio debugging tips
jvm-06.垃圾回收器
Leetcode topic resolution divide two integers
Operating mongodb in node
iNFTnews | 加密之家从宇宙寄来的明信片,你会收到哪一张?