LeeCode中如何定义常见的数据结构?

1. 单链表:

1.1 单链表结点:

// Definition for singly-linked list.
public static class ListNode {
    int val;
    ListNode next;

    ListNode(int x) {
        val = x;
    }
}

1.2 构建单链表:

1.2.1 ListNodeUtil类:

public static class ListNodeUtil {

    // 构建链表
    public static ListNode createLinkedList(int[] arr) { // 将输入的数组输入到链表中
        if (arr.length == 0) {
            return null;
        }
        ListNode head = new ListNode(arr[0]);
        ListNode current = head;
        for (int i = 1; i < arr.length; i++) {// 过程
            current.next = new ListNode(arr[i]);
            current = current.next;
        }
        return head;
    }

    // 打印链表
    public static void printLinkedList(ListNode head) { // 将链表结果打印
        ListNode current = head;
        while (current != null) {
            System.out.printf("%d -> ", current.val);
            current = current.next;
        }
        System.out.println("NULL");
    }
}

1.2.2 Main类:

import java.util.*;

public class Main {

    public static void main(String[] args) {
        int[] x = {1, 2, 3, 4, 5, 6};
        ListNode list = ListNodeUtil.createLinkedList(x);
        ListNodeUtil.printLinkedList(list);
    }
}

2. 二叉树:

2.1 二叉树结点:

// Definition for a binary tree node.
public static class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;

    TreeNode(int x) {
        val = x;
    }
}

2.2 构建二叉树:

2.2.1 TreeNodeUtil类:

import java.util.*;

public static class TreeNodeUtil {
    public static TreeNode arrayToTreeNode(Integer[] array) {
        if (array.length == 0) {
            return null;
        }
        TreeNode root = new TreeNode(array[0]);
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        boolean isLeft = true;
        for (int i = 1; i < array.length; i++) {
            TreeNode node = queue.peek();
            if (isLeft) {
                if (array[i] != null) {
                    node.left = new TreeNode(array[i]);
                    queue.offer(node.left);
                }
                isLeft = false;
            } else {
                if (array[i] != null) {
                    node.right = new TreeNode(array[i]);
                    queue.offer(node.right);
                }
                queue.poll();
                isLeft = true;
            }
        }
        return root;
    }
}

2.2.2 Main类:

import java.util.*;

public class Main {

    public static void main(String[] args) {
        Integer nums[] = {5, 4, 8, 11, null, 13, 4, 7, 2, null, null, null, 1};

        // 构造树的方法:
        TreeNode tree = TreeNodeUtil.arrayToTreeNode(nums);
        TreeOperation.show(tree);
    }
}

参考:

  1. LeetCode如何构建链表和树的测试用例
  2. Java一维数组转换二叉树结构
  3. Java 一维数组转换二叉树
  4. Java一维数组转换二叉树结构
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

dev_zyx

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值