package offer;
import java.util.LinkedList;
import java.util.Queue;
/**
* offer interview 23
*/
public class Test23 {
public static class BinaryTreeNode{
int value;
BinaryTreeNode left;
BinaryTreeNode right;
public BinaryTreeNode(){}
public BinaryTreeNode(int value){
this.value = value;
this.left = null;
this.right = null;
}
}
public static void printFromToBottom(BinaryTreeNode root){
if (null != root){
Queue<BinaryTreeNode> list = new LinkedList<>();
list.add(root);
BinaryTreeNode curNode;
while (!list.isEmpty()){
curNode = list.remove();
System.out.print(curNode.value + " ");
if (curNode.left != null){
list.add(curNode.left);
}
if (curNode.right != null){
list.add(curNode.right);
}
}
}
System.out.println();
}
public static void main(String[] args){
// 8
// / \
// 6 10
// / \ / \
// 5 7 9 11
BinaryTreeNode root = new BinaryTreeNode(8);
root.left = new BinaryTreeNode(6);
root.right = new BinaryTreeNode(10);
root.left.left = new BinaryTreeNode(5);
root.left.right = new BinaryTreeNode(7);
root.right.left = new BinaryTreeNode(9);
root.right.right = new BinaryTreeNode(11);
printFromToBottom(root);
// 1
// /
// 3
// /
// 5
// /
// 7
// /
// 9
BinaryTreeNode root2 = new BinaryTreeNode(1);
root2.left = new BinaryTreeNode(3);
root2.left.left = new BinaryTreeNode(5);
root2.left.left.left = new BinaryTreeNode(7);
root2.left.left.left.left = new BinaryTreeNode(9);
printFromToBottom(root2);
// 0
// \
// 2
// \
// 4
// \
// 6
// \
// 8
BinaryTreeNode root3 = new BinaryTreeNode(0);
root3.right = new BinaryTreeNode(2);
root3.right.right = new BinaryTreeNode(4);
root3.right.right.right = new BinaryTreeNode(6);
root3.right.right.right.right = new BinaryTreeNode(8);
printFromToBottom(root3);
}
}