null和没有一个元素的List不一样

题目描述

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));

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值