/**
* 从上到下打印二叉树(二叉树的层次遍历)
*
* @param root
*/
public void printFromTopToBottom(TreeNode root) {
if (null == root) return;
Queue<TreeNode> queue = new LinkedBlockingQueue<>();
queue.add(root);
while (!queue.isEmpty()) {
int size = queue.size();
for (int i = 0; i < size; i++) {
TreeNode r = queue.poll();
System.out.print(r.val);
if (r.left != null) queue.add(r.left);
if (r.right != null) queue.add(r.right);
}
}
}
剑指Offer学习-面试题32:从上到下打印二叉树(二叉树的层次遍历)
最新推荐文章于 2024-05-21 20:28:34 发布