题目描述
从上往下打印出二叉树的每个节点,同层节点从左至右打印。
思路:此题实际为二叉树的广度遍历,广度遍历必须借助其他的数据结构才能进行,比如最常见的Queue、ArrayList //(不能直接递)
扩展:
广度遍历顺序:1 2 3 4 5 6 7
深度遍历顺序:1 2 4 5 3 6 7
package test;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;
class TreeNode {
int val;
TreeNode left = null;
TreeNode right = null;
public TreeNode(int val) {
this.val = val;
}
}
public class Main {
public static void main(String[] args) {
TreeNode head=new TreeNode(1);//849
TreeNode second=new TreeNode(2);//850
TreeNode three=new TreeNode(3);//851
TreeNode four=new TreeNode(4);//852
TreeNode five=new TreeNode(5);//853
TreeNode six=new TreeNode(6);//854
TreeNode seven=new TreeNode(7);//855
head.right=three;
head.left=second;
second.right=five;
second.left=four;
three.right=seven;
three.left=six;
System.out.print("广度(或者宽度)优先遍历使用队列Queue:");//进去再弹出来
new Main().BroadFirstSearch(head);
System.out.println();
System.out.print("广度优先遍历使用数组ArrayList:");
new Main().PrintFromTopToBottom(head);
System.out.println();
System.out.print("深度优先遍历使用栈Stack:");
new Main().depthFirstSearch(head);
}
//广度优先遍历是使用队列实现的(//这里变成static,上面就不用了加new Main().了)
public void BroadFirstSearch(TreeNode nodeHead) {//谁先出来把谁的左右放进去
if(nodeHead==null) {
return;
}
Queue<TreeNode> myQueue=new LinkedList<>();
myQueue.add(nodeHead);//在容量已满的情况下,add() 方法会抛出IllegalStateException异常,offer() 方法只会返回 false 。
while(!myQueue.isEmpty()) {
TreeNode node=myQueue.poll();//poll:移除并返问队列头部的元素,如果队列为空,则返回null
//remove:移除并返回队列头部的元素,如果队列为空,则抛出一个NoSuchElementException异常
System.out.print(node.val+" ");
if(node.left!=null) {
myQueue.add(node.left); //广优先遍历,我们在这里采用每一行从左到右遍历
}
if(node.right!=null) {
myQueue.add(node.right);
}
}
}
//广度优先遍历使用arraylist模拟一个队列来存储相应的TreeNode
//public ArrayList<Integer> PrintFromTopToBottom(TreeNode nodeHead) {//谁先出来把谁的左右放进去
public void PrintFromTopToBottom(TreeNode nodeHead) {
//ArrayList<Integer> list = new ArrayList<>();
ArrayList<TreeNode> queue = new ArrayList<>();
if (nodeHead == null) {
return;
}
queue.add(nodeHead);
while (queue.size() != 0) {
TreeNode temp = queue.remove(0);//方法返回的值就是被删除的元素,如果索引值在实际的范围外,则会抛出IndexOutOfBoundsException异常
System.out.print(temp.val+" ");
if (temp.left != null){
queue.add(temp.left);
}
if (temp.right != null) {
queue.add(temp.right);
}
//list.add(temp.val);
}
//return list;
}
//深度优先遍历栈实现
public void depthFirstSearch(TreeNode nodeHead) {//谁先出来把谁的右左放进去
if(nodeHead==null) {
return;
}
Stack<TreeNode> myStack=new Stack<>();
myStack.add(nodeHead);
while(!myStack.isEmpty()) {
TreeNode node=myStack.pop(); //函数返回栈顶的元素,弹出栈顶元素
System.out.print(node.val+" ");
if(node.right!=null) {
myStack.push(node.right); //深度优先遍历,先遍历左边,后遍历右边,栈先进后出
}
if(node.left!=null) {
myStack.push(node.left);
}
}
}
}
这道题的解题模板:
import java.util.*;
/**
public class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null;
public TreeNode(int val) {
this.val = val;
}
}
*/
public class Solution {
public ArrayList<Integer> PrintFromTopToBottom(TreeNode root) {
ArrayList<Integer> list = new ArrayList<>();
if (root == null) {
return list;
}
Queue<TreeNode> queue = new LinkedList<>();
queue.add(root);
while (!queue.isEmpty()) {
TreeNode tem = queue.poll();
list.add(tem.val);
if (tem.left != null) {
queue.add(tem.left);
}
if (tem.right != null) {
queue.add(tem.right);
}
}
return list;
}
}