力扣生成链表和二叉树的测试代码

  • 根据力扣给出的用例,生成对应的链表和树,方便在IDEA上调试,不再需要手动new出一堆节点然后手动拼接
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
import java.util.Queue;

public class Solution {
    public static void main(String[] args) {
        ListNode head = generateList(new Integer[]{1, 10});
        TreeNode root = generateTree("1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3");
        // 检验是否生成的正确
        levelOrder(root);
    }

    // 生成树
    public static TreeNode generateTree(String arrayStr) {
        TreeNode dummyRoot = new TreeNode();
        // null作为虚拟根节点的左孩子
        arrayStr = "null," + arrayStr;
        String[] split = arrayStr.split(",");
        Queue<TreeNode> parentQueue = new LinkedList<>();
        parentQueue.add(dummyRoot);
        Queue<String> queue = new LinkedList<>(Arrays.asList(split));

        while (!queue.isEmpty()) {
            // parentQueue出一个
            TreeNode parent = parentQueue.remove();
            // queue出两个
            String leftStr = queue.remove();
            if (!"null".equals(leftStr)) {
                int value = Integer.parseInt(leftStr);
                parent.left = new TreeNode(value);
                parentQueue.add(parent.left);
            }
            if (queue.isEmpty()) break;
            String rightStr = queue.remove();
            if (!"null".equals(rightStr)) {
                int value = Integer.parseInt(rightStr);
                parent.right = new TreeNode(value);
                parentQueue.add(parent.right);
            }
        }

        return dummyRoot.right;
    }

    // 层序遍历
    public static void levelOrder(TreeNode root) {
        if (root == null) return;
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        System.out.print(root.val + " ");
        while (!queue.isEmpty()) {
            TreeNode remove = queue.remove();
            if (remove.left != null) {
                queue.add(remove.left);
                System.out.print(remove.left.val + " ");
            } else {
                System.out.print("null ");
            }
            if (remove.right != null) {
                queue.add(remove.right);
                System.out.print(remove.right.val + " ");
            } else {
                System.out.print("null ");
            }
        }
    }

    // 生成链表
    public static ListNode generateList(Integer[] array) {
        Collections.reverse(Arrays.asList(array));
        ListNode dummyHead = new ListNode();
        for (Integer integer : array)
            dummyHead.next = new ListNode(integer, dummyHead.next);

        return dummyHead.next;
    }
}

class ListNode {
    int val;
    ListNode next;

    ListNode() {
    }

    ListNode(int val) {
        this.val = val;
    }

    ListNode(int val, ListNode next) {
        this.val = val;
        this.next = next;
    }
}

class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;

    TreeNode() {
    }

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

    TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}
posted @ 2022-07-19 11:19  n1ce2cv  阅读(108)  评论(0编辑  收藏  举报