面试题23:二叉树:从上往下打印二叉树
来源:互联网 发布:山东二建继续教育网络 编辑:程序博客网 时间:2024/05/16 17:40
题目描述从上往下打印出二叉树的每个节点,同层节点从左至右打印。
思路:
这道题实质是考查树的遍历算法。从上到下打印二叉树的规律:每一次打印一个结点的时候,如果该结点有子结点, 则把该结点的子结点放到一个队列的末尾。接下来到队列的头部取出最早进入队列的结点,重复前面的打印操作,直至队列中所有的结点都被打印出来为止。
广度优先遍历。
import java.util.ArrayList;import java.util.LinkedList;/**public class TreeNode { int val = 0; TreeNode left = null; TreeNode right = null; public TreeNode(int val) { this.val = val; }}*/public class Solution { private ArrayList<Integer> list = new ArrayList<>(); public ArrayList<Integer> PrintFromTopToBottom(TreeNode root) { if(root == null) { return new ArrayList<>(); } LinkedList<TreeNode> linkedList = new LinkedList<>(); linkedList.add(root); PrintFromTopToBottom(linkedList); return list; } private void PrintFromTopToBottom(LinkedList<TreeNode> linkedList) { if(linkedList == null || linkedList.isEmpty()) { return; } while(!linkedList.isEmpty()) { TreeNode head = linkedList.poll(); list.add(head.val); if(head.left != null) { linkedList.add(head.left); } if(head.right != null) { linkedList.add(head.right); } } }}
涉及知识:
- 广度优先遍历
- 队列操作:LinkedList
public class LinkedList<E> extends AbstractSequentialList<E> implements List<E>, Deque<E>, Cloneable, java.io.Serializable
package java.util;public interface Queue<E> extends Collection<E> { boolean add(E e); /** * Inserts the specified element into this queue if it is possible to do * so immediately without violating capacity restrictions. * When using a capacity-restricted queue, this method is generally * preferable to {@link #add}, which can fail to insert an element only * by throwing an exception. * * @param e the element to add * @return {@code true} if the element was added to this queue, else * {@code false} * @throws ClassCastException if the class of the specified element * prevents it from being added to this queue * @throws NullPointerException if the specified element is null and * this queue does not permit null elements * @throws IllegalArgumentException if some property of this element * prevents it from being added to this queue */ boolean offer(E e); /** * Retrieves and removes the head of this queue. This method differs * from {@link #poll poll} only in that it throws an exception if this * queue is empty. * * @return the head of this queue * @throws NoSuchElementException if this queue is empty */ E remove(); /** * Retrieves and removes the head of this queue, * or returns {@code null} if this queue is empty. * * @return the head of this queue, or {@code null} if this queue is empty */ E poll(); /** * Retrieves, but does not remove, the head of this queue. This method * differs from {@link #peek peek} only in that it throws an exception * if this queue is empty. * * @return the head of this queue * @throws NoSuchElementException if this queue is empty */ E element(); /** * Retrieves, but does not remove, the head of this queue, * or returns {@code null} if this queue is empty. * * @return the head of this queue, or {@code null} if this queue is empty */ E peek();}
阅读全文
0 0
- 面试题23:从上往下打印二叉树
- 从上往下打印二叉树(面试题 23)
- 面试题23 从上往下打印二叉树
- 面试题23:从上往下打印二叉树
- 面试题23:从上往下打印二叉树
- 面试题23从上往下打印二叉树
- 面试题23_从上往下打印二叉树
- 面试题23从上往下打印二叉树
- 面试题:23从上往下打印二叉树
- 面试题23:从上往下打印二叉树
- 面试题23:从上往下打印二叉树
- 面试题23:从上往下打印二叉树
- 面试题23:从上往下打印二叉树
- 面试题23:从上往下打印二叉树
- 面试题23:从上往下打印二叉树
- 面试题23从上往下打印二叉树
- 面试题23:从上往下打印二叉树
- 面试题23—从上往下打印二叉树
- 数据库存储大数据量(千万条记录级别)数据的考虑要点
- win7系统运行极品飞车10出现卡顿该如何解决?
- 位置式 和 增量式 PID
- 练习94
- WebService 的No Relevant Sources Lines报错解决
- 面试题23:二叉树:从上往下打印二叉树
- java中四种引用类型(对象的强、软、弱和虚引用)
- 【模拟】CodeForces
- FindBugs规则整理
- spring redis 通过maven 引用
- 百度编辑器ueditor插入表格没有边框颜色的解决方法
- Java多语言编程-程序国际化
- 练习95
- Amndroid 弹幕Barrageview