创建含有某一深度上所有节点的链表,返回一个包含所有深度的链表的数组-算法设计

该文章提供了一个Java实现,用于处理二叉树的问题。主要功能包括判断是否为叶子节点、计算树的节点数、层次遍历、中序遍历、后序遍历以及前序遍历。此外,还提供了构建二叉树的方法,并以层次遍历的方式展示树的结构。
摘要由CSDN通过智能技术生成
/**
 * 输入:[1,2,3,4,5,null,7,8]
 * <p>
 * --------1
 * ======/  \
 * ======2--3
 * ====/ \    \
 * ===4==5=====7
 * ==/
 * =8
 * <p>
 * 输出:[[1],[2,3],[4,5,7],[8]]
 */

/**
 * 给定一棵二叉树,设计一个算法,创建含有某一深度上所有节点的链表(比如,若一棵树的深度为 D,
 * 则会创建出 D 个链表)。返回一个包含所有深度的链表的数组
 */

树结点


/**
 * @author yichuan
 */
public class TreeNode {
    public int val;
    public TreeNode left;
    public TreeNode right;

    public TreeNode(int val) {
        this.val = val;
    }


    public TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }

    @Override
    public String toString() {
        return "TreeNode{" +
                "val=" + val +
                ", left=" + left +
                ", right=" + right +
                '}';
    }

    public int getVal() {
        return val;
    }

    public void setVal(int val) {
        this.val = val;
    }

    public TreeNode getLeft() {
        return left;
    }

    public void setLeft(TreeNode left) {
        this.left = left;
    }

    public TreeNode getRight() {
        return right;
    }

    public void setRight(TreeNode right) {
        this.right = right;
    }
}

实现方法

/**
 * 给定一棵二叉树,设计一个算法,创建含有某一深度上所有节点的链表(比如,若一棵树的深度为 D,
 * 则会创建出 D 个链表)。返回一个包含所有深度的链表的数组
 */
public class Solution {
    /**
     * 是否为叶子结点
     *
     * @param treeNode
     * @return
     */
    public boolean isLeaveChild(TreeNode treeNode) {
        if (treeNode != null && treeNode.val != -1) {
            if (treeNode.left == null && treeNode.right == null) {
                return true;
            }
            return false;
        }
        return false;
    }


    /**
     * 求树的结点数
     *
     * @param treeNode
     * @return
     */
    int n = 0;

    public int nodeCount(TreeNode treeNode) {
        if (treeNode != null && treeNode.val != -1) {
            n += 1;
            System.out.println("是否是叶子结点:" + isLeaveChild(treeNode));
            System.out.println("" + treeNode.toString());
            nodeCount(treeNode.left);
            nodeCount(treeNode.right);

        }
        return n;

    }

    /**
     * 层次遍历
     */
    public void levelOrder(TreeNode treeNode) {
        TreeNode p = treeNode;
        LinkedBlockingQueue<TreeNode> queue = new LinkedBlockingQueue();
        System.out.println("\r\n层次遍历");
        while (p != null) {
            // 访问P结点
            System.out.print("\t" + p.val + "");
            if (p.getLeft() != null) {
                queue.add(p.getLeft());
            }
            if (p.getRight() != null) {
                queue.add(p.getRight());
            }
            //p 指向出队结点
            p = queue.poll();
        }
        System.out.println(" ");
    }

    /**
     * @param value
     * @return
     */
    private TreeNode buildTree(int[] value) {
        TreeNode p = new TreeNode(value[0]);
        TreeNode q = p;
        Queue<TreeNode> queue = new LinkedList<>();
        int i = 0;
        while (p != null && p.val != -1) {
            if (2 * i + 1 < value.length) {
                p.left = new TreeNode(value[2 * i + 1]);
                queue.add(p.left);
            }
            if (2 * i + 2 < value.length) {
                p.right = new TreeNode(value[2 * i + 2]);
                queue.add(p.right);
            }
            p = queue.poll();
            i += 1;
        }
        return q;
    }

    /**
     * 层次遍历建立二叉树,这里使用的递归的方法
     * <p>
     * 利用二叉树结点位置的性质构造树
     *
     * @param arrays
     * @param n
     * @return
     */

    public TreeNode BuildTree(int[] arrays, int n) {
        TreeNode treeNode = null;
        if (arrays.length == 0) {
            return null;
        }
        if (n < arrays.length) {
            int l = n * 2 + 1;
            int r = n * 2 + 2;
            treeNode = new TreeNode(arrays[n],
                    BuildTree(arrays, l),
                    BuildTree(arrays, r));
        }
        return treeNode;
    }


    /**
     * 中序序遍历
     *
     * @param treeNode
     */
    public void inorder(TreeNode treeNode) {
        if (treeNode != null) {
            inorder(treeNode.left);
            System.out.println(treeNode.val);
            inorder(treeNode.right);
        }
    }

    /**
     * 中序遍历
     *
     * @param treeNode
     */
    public void postorder(TreeNode treeNode) {
        if (treeNode != null) {
            inorder(treeNode.left);
            inorder(treeNode.right);
            System.out.println(treeNode.val);
        }
    }

    //前序遍历
    public void preorder(TreeNode treeNode) {
        if (treeNode != null) {
            System.out.println(treeNode.val);
            inorder(treeNode.left);
            inorder(treeNode.right);

        }
    }

    public static void main(String[] args) {
        int[] arrays = {1, 2, 3, 4, 5, -1, 7, 8};
        Solution solution = new Solution();
        //createTree
        TreeNode treeNode = solution.BuildTree(arrays, 0);
        System.out.println("" + treeNode);
        int n = solution.nodeCount(treeNode);
        System.out.println("count:" + n);

        solution.levelOrder(treeNode);
        System.out.println("中序遍历");
        solution.inorder(treeNode);
        System.out.println("前序遍历");
        solution.preorder(treeNode);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值