剑指offer第二十二题从上往下打印二叉树

题目描述

从上往下打印出二叉树的每个节点,同层节点从左至右打印。

思路:此题实际为二叉树的广度遍历,广度遍历必须借助其他的数据结构才能进行,比如最常见的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;
    }
}

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值