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

之子型打印二叉树

  具体解法见书上,直接上代码吧。

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<BinaryTreeNode>[] stacks = new Stack[2];
        stacks[0]=new Stack<BinaryTreeNode>();
        stacks[1]=new Stack<BinaryTreeNode>();
        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<Integer> list = new ArrayList<Integer>();
        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<Integer> list) {
        if (root != null) {
            list.add(root.value);
            getPreOrderRecursively(root.leftNode, list);
            getPreOrderRecursively(root.rightNode, list);
        }
    }


    //返回中序遍历
    public int[] getInOrder(BinaryTreeNode root) {
        ArrayList<Integer> list = new ArrayList<Integer>();
        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<Integer> 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;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值