剑指offer 面试题23 从上往下打印二叉树 Java实现

package sword.to.offer;

import java.util.LinkedList;
import java.util.Queue;
public class PrintFromTopToBottomOfBinaryTree {

	class BinaryTreeNode{
		public BinaryTreeNode(int value) {
			this.mValue=value;
			this.mLeft=null;
			this.mRight=null;
		}
		public BinaryTreeNode(int value,BinaryTreeNode left,BinaryTreeNode right) {

			mValue=value;
			mLeft=left;
			mRight=right;
		}
		int mValue;
		BinaryTreeNode mLeft;
		BinaryTreeNode mRight;
	}
	public static void PrintFromTopToBottom(BinaryTreeNode binaryTreeNode){
	
		if(binaryTreeNode==null)
			return;
		Queue queue = new LinkedList();
		queue.offer(binaryTreeNode);
		while(queue.size()!=0){
			BinaryTreeNode node=queue.peek();
			queue.poll();
			System.out.println(""+node.mValue);
			if(node.mLeft!=null)
				queue.offer(node.mLeft);
			if(node.mRight!=null)
				queue.offer(node.mRight);
		}
	}
	public static void main(String[] args) {
//		IntStream is=IntStream.builder().add(20).build();
		
		
		//方式一  就是把BinaryTreeNode修改成static,静态内部类。此处不再修改
		//方式二
		PrintFromTopToBottomOfBinaryTree binaryTree=new PrintFromTopToBottomOfBinaryTree();
		BinaryTreeNode binaryTreeNode5= binaryTree.new BinaryTreeNode(5);
		BinaryTreeNode binaryTreeNode7= binaryTree.new BinaryTreeNode(7);
		BinaryTreeNode binaryTreeNode9= binaryTree.new BinaryTreeNode(9);
		BinaryTreeNode binaryTreeNode11= binaryTree.new BinaryTreeNode(11);
		BinaryTreeNode binaryTreeNode6= binaryTree.new BinaryTreeNode(6,binaryTreeNode5,binaryTreeNode7);
		BinaryTreeNode binaryTreeNode10= binaryTree.new BinaryTreeNode(10,binaryTreeNode9,binaryTreeNode11);
		BinaryTreeNode binaryTreeNode8= binaryTree.new BinaryTreeNode(8,binaryTreeNode6,binaryTreeNode10);
		PrintFromTopToBottom(binaryTreeNode8);
		
		
	}
}

你可能感兴趣的:(剑指offer代码Java实现)