層次遍歷,遇到偶數(shù)行反轉(zhuǎn)
// 270 t103 鋸齒遍歷
public List>zigzagLevelOrder(TreeNode root) {boolean isLeftToR=true;
Queuequeue= new LinkedList<>();
ArrayList>res = new ArrayList<>();
if (root==null){return res;
}
queue.offer(root);
while (!queue.isEmpty()){int len=queue.size();
ArrayListlist = new ArrayList<>();
while (len>0){TreeNode poll = queue.poll();
list.add(poll.val);
if (poll.left!=null){queue.offer(poll.left);
}
if (poll.right!=null){queue.offer(poll.right);
}
len--;
}
if (!isLeftToR){Collections.reverse(list);
}
res.add(list);
isLeftToR=!isLeftToR;
}
return res;
}
你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機(jī)房具備T級(jí)流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級(jí)服務(wù)器適合批量采購(gòu),新人活動(dòng)首月15元起,快前往官網(wǎng)查看詳情吧