此问题与功能public List inorderTraversal ()有关。
假设根为null,返回的List
为空,即size =0。我现在很困惑,因为它与Collections.emptyList()相同。我们什么时候应该使用Collections.emptyList?这是inorderTraversal
使用它而不是大小为0的列表的正确位置吗?
最佳答案
public static void main(String[] args) {
List l = Collections.emptyList();
l.clear(); // No NPE
List p = null;
p.clear(); // NPE
}
因此,更喜欢返回emptyList而不是返回null。