日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【剑指offer】面试题32:从上到下打印二叉树(java)

發布時間:2024/7/19 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【剑指offer】面试题32:从上到下打印二叉树(java) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

從上往下打印二叉樹的每個節點,同一層的節點按照從左到右的順序打印。例如輸入下圖的二叉樹,則一次打印出8,6,10,5,7,9,11。

思路:利用隊列,將左右子樹加入隊列末尾,取出結點

代碼:

package offer;

import java.util.LinkedList;
import java.util.Queue;

class BineryTree
{
?? ?int val;
?? ?BineryTree left = null;
?? ?BineryTree right = null;
?? ?BineryTree(int val)
?? ?{
?? ??? ?this.val = val;
?? ?}
}
public class ti32 {
?? ?static void BineryTreeToQueue(BineryTree head)
?? ?{
?? ??? ?if(head==null)
?? ??? ?{
?? ??? ??? ?return;
?? ??? ?}
?? ??? ?Queue<BineryTree> queue = new LinkedList<BineryTree>();
?? ??? ?queue.add(head);
?? ??? ?while(!queue.isEmpty())
?? ??? ?{
?? ??? ??? ?BineryTree x = queue.poll();
?? ??? ??? ?System.out.println(x.val);
?? ??? ??? ?if(x.left!=null)
?? ??? ??? ?{
?? ??? ??? ??? ?queue.add(x.left);
?? ??? ??? ?}
?? ??? ??? ?if(x.right!=null)
?? ??? ??? ?{
?? ??? ??? ??? ?queue.add(x.right);
?? ??? ??? ?}
?? ??? ?}
?? ?}
?? ?public static void main(String[] args)
?? ?{
?? ??? ?BineryTree a = new BineryTree(8);
?? ??? ?BineryTree b = new BineryTree(6);
?? ??? ?BineryTree c = new BineryTree(10);
?? ??? ?BineryTree d = new BineryTree(5);
?? ??? ?BineryTree e = new BineryTree(7);
?? ??? ?BineryTree f = new BineryTree(9);
?? ??? ?BineryTree g = new BineryTree(11);
?? ??? ?a.left = b;
?? ??? ?a.right = c;
?? ??? ?b.left = d;
?? ??? ?b.right = e;
?? ??? ?c.left = f;
?? ??? ?c.right = g;
?? ??? ?BineryTreeToQueue(a);
?? ?}
}
題目三
請實現一個函數按照之字形打印二叉樹,即第一行按照從左到右的順序打印,第二層按照從右至左的順序打印,第三行按照從左到右的順序打印,其他行以此類推。

代碼:

package offer;

import java.util.ArrayList;
import java.util.Stack;

class BineryTree
{
?? ?int val;
?? ?BineryTree left = null;
?? ?BineryTree right = null;
?? ?BineryTree(int val)
?? ?{
?? ??? ?this.val = val;
?? ?}
}
public class ti32 {
?? ?static ArrayList<ArrayList<Integer>> BineryTreeToQueue(BineryTree head)
?? ?{
?? ??? ?ArrayList<ArrayList<Integer>> alist = new ArrayList<>();
?? ??? ??
?? ??? ?if(head==null)
?? ??? ?{
?? ??? ??? ?return alist;
?? ??? ?}
?? ??? ?Stack<BineryTree> stack1 = new Stack<BineryTree>();
?? ??? ?Stack<BineryTree> stack2 = new Stack<BineryTree>();
?? ??? ?stack1.add(head);
?? ??? ?while(!stack1.empty()||!stack2.empty())
?? ??? ?{
?? ??? ??? ?ArrayList<Integer> list = new ArrayList<>();
?? ??? ??? ??? ?if(!stack1.empty())
?? ??? ??? ??? ?{
?? ??? ??? ??? ??? ?while(!stack1.empty())
?? ??? ??? ??? ??? ?{
?? ??? ??? ??? ??? ??? ?BineryTree x = stack1.pop();
?? ??? ??? ??? ??? ??? ?list.add(x.val);
?? ??? ??? ??? ??? ??? ?if(x.left!=null)
?? ??? ??? ??? ??? ??? ?{
?? ??? ??? ??? ??? ??? ??? ?stack2.add(x.left);
?? ??? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ??? ?if(x.right!=null)
?? ??? ??? ??? ??? ??? ?{
?? ??? ??? ??? ??? ??? ??? ?stack2.add(x.right);
?? ??? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ?
?? ??? ??? ??? ?}
?? ??? ??? ??? ?else
?? ??? ??? ??? ?{
?? ??? ??? ??? ??? ?while(!stack2.empty())
?? ??? ??? ??? ??? ?{
?? ??? ??? ??? ??? ??? ?BineryTree x = stack2.pop();
?? ??? ??? ??? ??? ??? ?list.add(x.val);
?? ??? ??? ??? ??? ??? ?if(x.right!=null)
?? ??? ??? ??? ??? ??? ?{
?? ??? ??? ??? ??? ??? ??? ?stack1.add(x.right);
?? ??? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ??? ?if(x.left!=null)
?? ??? ??? ??? ??? ??? ?{
?? ??? ??? ??? ??? ??? ??? ?stack1.add(x.left);
?? ??? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ?
?? ??? ??? ??? ?}
?? ??? ??? ??? ?alist.add(list);
?? ??? ??? ?}
?? ??? ?return alist;
?? ??? ?}
?? ?
?? ?public static void main(String[] args)
?? ?{
?? ??? ?BineryTree a = new BineryTree(8);
?? ??? ?BineryTree b = new BineryTree(6);
?? ??? ?BineryTree c = new BineryTree(10);
?? ??? ?BineryTree d = new BineryTree(5);
?? ??? ?BineryTree e = new BineryTree(7);
?? ??? ?BineryTree f = new BineryTree(9);
?? ??? ?BineryTree g = new BineryTree(11);
?? ??? ?a.left = b;
?? ??? ?a.right = c;
?? ??? ?b.left = d;
?? ??? ?b.right = e;
?? ??? ?c.left = f;
?? ??? ?c.right = g;
?? ??? ?ArrayList<ArrayList<Integer>> alist = BineryTreeToQueue(a);
?? ??? ?for(int i=0;i<alist.size();i++)
?? ??? ?{
?? ??? ??? ?for(int j=0;j<alist.get(i).size();j++)
?? ??? ??? ?{
?? ??? ??? ??? ?System.out.print(alist.get(i).get(j)+" ");
?? ??? ??? ?}
?? ??? ??? ?System.out.println();
?? ??? ?}
?? ?}
}
?

總結

以上是生活随笔為你收集整理的【剑指offer】面试题32:从上到下打印二叉树(java)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。