如何修復這個LevelOrder遍歷?錯誤的LevelOrder遍歷
public static void LevelOrder(TreeNode root){
Queue <TreeNode> q = new LinkedList <>();
TreeNode tmp=root;
q.add(tmp);
while (!q.isEmpty()) {
System.out.print(q.remove().data + " ");
if (tmp.left!=null) {
q.add(tmp.left);
}
if (tmp.left!=null) {
q.add(tmp.right);
}
tmp = q.peek();
}
}
爲什麼在隊列中添加tmp.right元素時檢查temp.left!= null? – attaboy182