# How to build test cases of linked list and tree by LeetCode

When you brush the list and tree in the leetcode topic, it's hard to avoid inconvenient debugging. Because I am used to debugging in IDEA to analyze errors, this paper will implement debugging of linked list and number in leetcode.

Code directly.

``````public static class ListNode{
int val;
ListNode next;
ListNode(int x) { val = x; }//Definition of linked list in Leetcode
}
private static ListNode createLinkedList(int[] arr) {//Input the input array into the linked list
if (arr.length == 0) {
return null;
}
for (int i = 1; i < arr.length; i++) {//process
current.next = new ListNode(arr[i]);
current = current.next;
}
}
while (current!=null){
System.out.printf("%d -> ",current.val);
current = current.next;
}
System.out.println("NULL");
}
public static void main(String[] args) {
int[] x = {1,2,3,4,5,6};
}
``````
• 1
• 2
• 3
• 4
• 5
• 6
• 7
• 8
• 9
• 10
• 11
• 12
• 13
• 14
• 15
• 16
• 17
• 18
• 19
• 20
• 21
• 22
• 23
• 24
• 25
• 26
• 27
• 28
• 29
• 30
• 31

The printed result is:

## tree

Code directly.

``````public static class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
}
public static TreeNode constructTree(Integer[] nums){
if (nums.length == 0) return new TreeNode(0);
TreeNode root = new TreeNode(nums[0]);
nodeQueue.offer(root);
TreeNode cur;
// Record the number of nodes in the current row (note that it is not necessarily the power of 2, but the number of non empty nodes in the previous row multiplied by 2)
int lineNodeNum = 2;
// Record the starting position of the number in the current row in the array
int startIndex = 1;
// Record the number of remaining elements in the array
int restLength = nums.length - 1;
while(restLength > 0) {
// Only the last line can be dissatisfied, and the rest must be full
//            //If the number of input arrays is wrong, jump out of the program directly
//            if (restLength < lineNodeNum) {
//                System.out.println("Wrong Input!");
//                return new TreeNode(0);
//            }
for (int i = startIndex; i < startIndex + lineNodeNum; i = i + 2) {
// It indicates that the number in nums has been used up. At this time, you should stop traversing and return to root directly
if (i == nums.length) return root;
cur = nodeQueue.poll();
if (nums[i] != null) {
cur.left = new TreeNode(nums[i]);
nodeQueue.offer(cur.left);
}
// As above, it means that the number in nums has been used up. At this time, the traversal should be stopped and root can be returned directly
if (i + 1 == nums.length) return root;
if (nums[i + 1] != null) {
cur.right = new TreeNode(nums[i + 1]);
nodeQueue.offer(cur.right);
}
}
startIndex += lineNodeNum;
restLength -= lineNodeNum;
lineNodeNum = nodeQueue.size() * 2;
}
return root;
}
public static void preOrder(TreeNode root) {//Preorder arrangement
if (root == null) return;
System.out.print(root.val + " ");
preOrder(root.left);
preOrder(root.right);
}
public static void midOrder(TreeNode root) {//Ordinal alignment
if (root == null) return;
midOrder(root.left);
System.out.print(root.val + " ");
midOrder(root.right);
}
public static void aftOrder(TreeNode root) {//Post order arrangement
if (root == null) return;
aftOrder(root.left);
aftOrder(root.right);
System.out.print(root.val + " ");
}
public static void main(String[] args) {
Integer[] nums = {1,2,2,3,3,3,3};
TreeNode tree=constructTree(nums);
preOrder(tree);
System.out.println();
midOrder(tree);
System.out.println();
aftOrder(tree);
}
``````
• 1
• 2
• 3
• 4
• 5
• 6
• 7
• 8
• 9
• 10
• 11
• 12
• 13
• 14
• 15
• 16
• 17
• 18
• 19
• 20
• 21
• 22
• 23
• 24
• 25
• 26
• 27
• 28
• 29
• 30
• 31
• 32
• 33
• 34
• 35
• 36
• 37
• 38
• 39
• 40
• 41
• 42
• 43
• 44
• 45
• 46
• 47
• 48
• 49
• 50
• 51
• 52
• 53
• 54
• 55
• 56
• 57
• 58
• 59
• 60
• 61
• 62
• 63
• 64
• 65
• 66
• 67
• 68
• 69
• 70
• 71
• 72
• 73
• 74
• 75
• 76
• 77
• 78
• 79
• 80

The printed result is:

I used the code of a blogger for reference, but I forgot which one. If the blogger saw it, comment on it, I'll mark it, MoMA da.

223 original articles published, 159 praised, 220000 visitors+

Tags: REST

Posted on Fri, 14 Feb 2020 09:14:45 -0500 by Zaxnyd