具体解法见书上,直接上代码吧。
import java.util.ArrayList;
import java.util.Stack;
public class Algorithm32_2 {
//之字形打印二叉树
public static void main(String[] args) {
int[] preOrder = {1, 2, 4, 8, 9, 5, 10, 11, 3, 6, 12, 13, 7, 14, 15};
int[] inOrder = {8, 4, 9, 2, 10, 5, 11, 1, 12, 6, 13, 3, 14, 7, 15};
BinaryTree binaryTree = new BinaryTree(preOrder, inOrder);
BinaryTreeNode root = binaryTree.construct();
printByRow2(root);
}
public static void printByRow2(BinaryTreeNode root) {
if(root==null){
System.out.println("树为空");
return;
}
Stack[] stacks = new Stack[2];
stacks[0]=new Stack();
stacks[1]=new Stack();
int current = 0;
int next = 1;
stacks[current].push(root);
while (!(stacks[0].empty() && stacks[1].empty())) {
BinaryTreeNode node = stacks[current].pop();
System.out.print(node.value);
System.out.print(" ");
//将下一层的节点放入栈
if(current==0){
if(node.leftNode!=null)
stacks[next].push(node.leftNode);
if(node.rightNode!=null)
stacks[next].push(node.rightNode);
}else{
if(node.rightNode!=null)
stacks[next].push(node.rightNode);
if(node.leftNode!=null)
stacks[next].push(node.leftNode);
}
if(stacks[current].empty()){
System.out.println();
next=1-next;
current=1-current;
}
}
}
}
使用二叉树的前序遍历数组和中序遍历数组构建二叉树
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class BinaryTree {
//二叉树类
private int[] preOrder;
private int[] inOrder;
public BinaryTree(int[] preOrder, int[] inOrder) {
this.preOrder = preOrder;
this.inOrder = inOrder;
}
//由二叉树的前序和中序序列构造二叉树
public BinaryTreeNode construct() {
if (preOrder == null || inOrder == null ||
this.preOrder.length <= 0 || inOrder.length <= 0 || preOrder.length != inOrder.length) {
return null;
}
int length = preOrder.length;
return constructNode(preOrder, inOrder, 0,
length - 1, 0, length - 1);
}
private BinaryTreeNode constructNode(int[] preorder, int[] inorder,
int startPreorderIndex, int endPreorderIndex, int startInorderIndex,
int endInorderIndex) {
//当前节点的值
int rootValue = preorder[startPreorderIndex];
//新建节点
BinaryTreeNode root = new BinaryTreeNode(rootValue);
//判断节点是否是叶子节点,如果是,返回节点
if (startPreorderIndex == endPreorderIndex) {
if (startInorderIndex == endInorderIndex &&
inorder[startInorderIndex] == preorder[startPreorderIndex]) {
return root;
} else {
System.out.println("二叉树构建错误");
}
}
int rootInorderIndex = startInorderIndex;
//找到中序遍历中根节点所在位置
while (rootInorderIndex <= endInorderIndex && inorder[rootInorderIndex] != rootValue) {
++rootInorderIndex;
}
//左子树的长度
int leftLength = rootInorderIndex - startInorderIndex;
int leftPreorderEndIndex = startPreorderIndex + leftLength;
//递归构造左子树
if (leftLength > 0) {
root.leftNode = constructNode(preorder, inorder,
startPreorderIndex + 1, leftPreorderEndIndex, startInorderIndex,
rootInorderIndex - 1);
}
//递归构造右子树
if (leftLength < endInorderIndex - startInorderIndex) {
root.rightNode = constructNode(preorder, inorder,
leftPreorderEndIndex + 1, endPreorderIndex,
rootInorderIndex + 1, endInorderIndex);
}
return root;
}
//返回前序遍历
public int[] getPreOrder(BinaryTreeNode root) {
ArrayList list = new ArrayList();
getPreOrderRecursively(root, list);
int[] pre = new int[list.size()];
for (int i = 0; i < list.size(); i++) {
pre[i] = list.get(i);
}
return pre;
}
//前序遍历的递归函数
public void getPreOrderRecursively(BinaryTreeNode root, ArrayList list) {
if (root != null) {
list.add(root.value);
getPreOrderRecursively(root.leftNode, list);
getPreOrderRecursively(root.rightNode, list);
}
}
//返回中序遍历
public int[] getInOrder(BinaryTreeNode root) {
ArrayList list = new ArrayList();
getInOrderRecursively(root, list);
int[] in = new int[list.size()];
for (int i = 0; i < list.size(); i++) {
in[i] = list.get(i);
}
return in;
}
//中序遍历的递归函数
public void getInOrderRecursively(BinaryTreeNode root, ArrayList list) {
if (root != null) {
getInOrderRecursively(root.leftNode, list);
list.add(root.value);
getInOrderRecursively(root.rightNode, list);
}
}
//打印二叉树的前序和中序遍历数组
public void printBinaryTree(int[] pre, int[] in) {
System.out.print("二叉树的前序遍历:");
System.out.println(Arrays.toString(pre));
System.out.print("二叉树的中序遍历:");
System.out.println(Arrays.toString(in));
}
}
public class BinaryTreeNode {
int value=-1;
BinaryTreeNode leftNode;
BinaryTreeNode rightNode;
public BinaryTreeNode(int v){
value=v;
}
}