What the difference between using q != null
and !q.isEmpty()
?
when I used q!=null in the following code, it's not going to be compiled. But !q.isEmpty() works pretty well.
java
Queue<TreeNode[]> q=new LinkedList<>();
q.add(new TreeNode[]{t1, t2});
while(q!=null){ // is not complied
TreeNode[] t=q.remove();
if(t[0]==null || t[1]==null) continue;
t[0].val+=t[1].val;
if(t[0].left==null) t[0].left=t[1].left;
else q.add(new TreeNode[]{t[0].left, t[1].left});
if(t[0].right==null) t[0].right=t[1].right;
else q.add(new TreeNode[]{t[0].right, t[1].right});
}