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

要判断一个List<String>中是否存在相同的元素,可以使用HashSet来实现。HashSet是一种不允许存在重复元素的集合,它底层是通过哈希表来存储元素的。具体操作步骤如下: 1. 创建一个空的HashSet对象,用来存放List中的元素; 2. 遍历List中的每个元素,将其依次添加到HashSet中; 3. 添加元素时,HashSet会自动判断是否已存在相同的元素,如果存在则不添加; 4. 判断HashSet的大小是否和原始List的大小相等,如果不相等,则说明存在相同的元素;否则,不存在相同的元素。 示例代码如下所示: ```java import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set; public class Main { public static void main(String[] args) { List<String> list = new ArrayList<>(); list.add("apple"); list.add("orange"); list.add("banana"); list.add("apple"); Set<String> set = new HashSet<>(list); if (set.size() < list.size()) { System.out.println("List中存在相同的元素"); } else { System.out.println("List中不存在相同的元素"); } } } ``` 在上述代码中,我们将List中的元素添加到HashSet中,然后比较HashSet的大小与List的大小。如果HashSet的大小小于List的大小,说明存在相同的元素,反之则不存在相同的元素。请注意,这里的List<String>和HashSet<String>都是泛型的用法,可以按照实际需求进行更改。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [【Robotframework】列表List的常用操作.pdf](https://download.csdn.net/download/qq_43934844/87511216)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [C#中查找Dictionary中重复值的方法](https://download.csdn.net/download/weixin_38660069/14873061)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [map用法及对List<对象>分组成Map<String,List<对象>>的方案](https://blog.csdn.net/qq_41429436/article/details/121989187)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值