如何修复这个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