亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

二叉樹按層遍歷打印

標簽:
Java 面試

二叉树按层进行遍历,例如:

             ①
         ②       ③
       ④   ⑤    ⑥     进行按层遍历的话打印就是:

1

2 3

4 5 6

思路:

      用一个current来表示当前指针,用nextLastRight来表示最右节点的指针,例如,current一开始指向1,而下一行的nextLastRight指针指向子节点的最右边,假如没有右子树,nextLastRight就指向左子树。

下面看看代码:

package com.lxj.binarytree;

import java.util.Queue;
import java.util.Scanner;
import java.util.concurrent.ArrayBlockingQueue;

public class Tree {
    
	private Tree left;
	private Tree right;
	private int value;
	
	public int getValue() {
		return this.value;
	}
	
	public void setValue(int value) {
		this.value = value;
	}
	
	public void setLeft(Tree left) {
		this.left = left;
	}

	public void setRight(Tree right) {
		this.right = right;
	}

	public Tree getLeft() {
		return this.left;
	}
	
	public Tree getRight() {
		return this.right;
	}

	public static void main(String []args) throws InterruptedException {
		Test test = new Test();
		Tree head = new Tree();
		test.buildTree(head);
//		System.out.println(head);
//		test.pre(head);
		//二叉树按层遍历
		test.layerForeach(head);
//		test.pre(head);
	}
	
}
/*二叉树宽度按层遍历
             ①
         ②       ③
       ④   ⑤    ⑥  0  
      0 0 0 0  0 0 
        1
		2
		4
		-1
		-1
		5
		-1
		-1
		3
		6
		-1
		-1
		-1
*/
class Test{
	  public void layerForeach(Tree head) throws InterruptedException {
		  ArrayBlockingQueue<Tree> queue = new ArrayBlockingQueue<>(20);
		  Tree curr = head;
		  Tree nextLast = head;
		  queue.put(head);
		   while(head != null) {
			   curr = head;
			   if(head.getLeft() != null) {
				   queue.put(head.getLeft());
			   }
			   if(head.getRight() != null) {
				   queue.put(head.getRight());
			   }
			   //如果当前节点等于最右节点则进行换行
			   int temp = queue.poll().getValue();
			   //假如是最右节点则进行换行
			   if(curr == nextLast) {
				  if(temp != 0) {
					  System.out.println(temp);
				  }
				   //找到最右节点
				   if(curr.getRight() != null) {
					   nextLast = curr.getRight();
				   }else {
					   nextLast = curr.getLeft();
				   }
			   }else {
				   if(temp != 0) {
				     System.out.print(temp + "  ");
				   }
			   }
			   //更新head值,但不出队列
			   head = queue.peek();
		   }
	  }
	  
    //构建二叉树,0代表为空
	public void buildTree(Tree head) {
		Scanner sc = new Scanner(System.in);
		int t;
		if(( t = sc.nextInt()) != -1) {
			head.setValue(t);
			head.setLeft(new Tree());
			head.setRight(new Tree());
			buildTree(head.getLeft()); 
			buildTree(head.getRight()); 
		}else {
			head = null;
		}
	}
	
	//二叉树的先序遍历
	public void pre(Tree head) {
		if(head != null) {
			System.out.println(head.getValue());
			pre(head.getLeft());
			pre(head.getRight()); 
		}
	}
}

https://img1.sycdn.imooc.com//5bb3598000014e7411120434.jpg

我这里构建二叉树是以-1为结束标志的,所以无子节点用-1代替。







點擊查看更多內容
3人點贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優質文章

正在加載中
JAVA開發工程師
手記
粉絲
7794
獲贊與收藏
665

關注作者,訂閱最新文章

閱讀免費教程

感謝您的支持,我會繼續努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦
今天注冊有機會得

100積分直接送

付費專欄免費學

大額優惠券免費領

立即參與 放棄機會
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號

舉報

0/150
提交
取消