剑指Offer 32题题目二(第二小题)java

题目:分行从上到下打印二叉树

  和32题第一小题相似,但是加入了一个变量 toBePrint来记录这一行的节点数,加入变量nextRow来记录下一行的节点数。在打印完一行后,令toBePrint=nextRow。为了方便测试,在同一个包下定义二叉树类BinaryTree和二叉树节点类BinaryTreeCode。
  使用前序遍历和中序遍历来构建二叉树,就是第七题的做法。需要给出二叉树的前序和中序遍历数组。

	import java.util.LinkedList;
	import java.util.Queue;
	
	public class Algorithm32_1 {
	    //分行从上到下打印二叉树
	    public static void main(String[] args){
	        int[] preOrder={8,6,5,7,10,9,11};
	        int[] inOrder={5,6,7,8,9,10,11};
	        BinaryTree binaryTree=new BinaryTree(preOrder,inOrder);
	        BinaryTreeNode root=binaryTree.construct();
	        printByRow1(root);
	    }
	    public static void printByRow1(BinaryTreeNode root){
	        if(root==null)
	            return;
	        Queue queue = new LinkedList<>();
	        queue.offer(root);
	        int nextRow=0;
	        int toBePrint=1;
	        while (!queue.isEmpty()){
	            BinaryTreeNode ptr=queue.poll();
	            System.out.print(ptr.value);
	            System.out.print(" ");
	            if(ptr.leftNode!=null){
	                queue.offer(ptr.leftNode);
	                nextRow++;
	            }
	            if(ptr.rightNode!=null){
	                queue.offer(ptr.rightNode);
	                nextRow++;
	            }
	            toBePrint--;
	            if(toBePrint==0){
	                System.out.println();
	                toBePrint=nextRow;
	            }
	        }
	
	    }
	}

构建二叉树的类:

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;
    }
}

你可能感兴趣的:(java,算法,剑指Offer)