思路:
使用队列进行遍历,先进入根,然后出队时让他们的子节点进入队列
代码:
public class BinaryTree {
public class leverOder(TreeNode root) {
queue<TreeNode> queue = new LinkedList<>();
if (root != null) {
queue.offer(root);
}
while (!queue.isEmpty()) {//获取队头元素
TreeNode topNode = queue.poll();
Syetem.out.println(topNode.val);
if (topNode.left != null) {
queue.offer(topNode.left);
}
if (topNode.right != null) {
queue.offer(topNode.right);
}
}
}
}