题目描述
LeetCode链接:https://leetcode.com/problems/encode-n-ary-tree-to-binary-tree
代码
由于本题应该算一道非常经典的LeetCode题目,因此应该很容易就找到这道题的详细题解,本题我就直接把代码放上来然后重点讲一下比较坑的点了。
我原本的代码:
/*
// Definition for a Node.
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;
}
};
*/
/**
* Definition for a binary tree node.
* public 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 = en1(root.children);
return head;
}
private TreeNode en1(List<Node> children){
TreeNode head = null;
TreeNode cur = null;
if(children == null){
return null;
}
for(Node child:children){
TreeNode tNode = new TreeNode(child.val);
if(head == null){
head = tNode;
}
else{
cur.right = tNode;
}
cur = tNode;
cur.left = en1(child.children);
}
return head;
}
// Decodes your binary tree to an n-ary tree.
public Node decode(TreeNode root) {
if(root == null){
return null;
}
Node head = new Node(root.val);
head.children = de1(root.left);
return head;
}
public List<Node> de1(TreeNode root) {
if(root == null){
return null;
}
List<Node> children = new ArrayList<>();
TreeNode cur = root;
while (cur != null){
Node tNode = new Node(cur.val);
tNode.children = de1(cur.left);
children.add(tNode);
cur = cur.right;
}
return children;
}
}
// Your Codec object will be instantiated and called as such:
// Codec codec = new Codec();
// codec.decode(codec.encode(root));
报错信息:
java.lang.NullPointerException: Cannot invoke "java.util.List.size()" because "<local4>.children" is null at line 134, Serializer.serializeRoot at line 188, __Driver__.main
错误原因:
de1()方法里,判断root是否为空的习惯没问题,但是这个地方,如果root为空,那么返回的是一个空链表,注意空链表也是链表,空链表不是null,所以这个地方把if判断root是否为空的步骤去掉就运行通过了
正确代码:
/*
// Definition for a Node.
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;
}
};
*/
/**
* Definition for a binary tree node.
* public 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 = en1(root.children);
return head;
}
private TreeNode en1(List<Node> children){
TreeNode head = null;
TreeNode cur = null;
if(children == null){
return null;
}
for(Node child:children){
TreeNode tNode = new TreeNode(child.val);
if(head == null){
head = tNode;
}
else{
cur.right = tNode;
}
cur = tNode;
cur.left = en1(child.children);
}
return head;
}
// Decodes your binary tree to an n-ary tree.
public Node decode(TreeNode root) {
if(root == null){
return null;
}
Node head = new Node(root.val);
head.children = de1(root.left);
return head;
}
public List<Node> de1(TreeNode root) {
List<Node> children = new ArrayList<>();
TreeNode cur = root;
while (cur != null){
Node tNode = new Node(cur.val);
tNode.children = de1(cur.left);
children.add(tNode);
cur = cur.right;
}
return children;
}
}
// Your Codec object will be instantiated and called as such:
// Codec codec = new Codec();
// codec.decode(codec.encode(root));