0%

leetCode树和链表工具

文章字数:481,阅读全文大约需要1分钟
网上找的leetCode题目字符串转树和链表的工具,来自原文

一、工具

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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package com.colin.tool;

import java.util.List;
import java.util.Queue;
import java.util.concurrent.LinkedBlockingQueue;

/**
* LeetCode工具
*/
public class LeetCode {

public static class ListNode {
public int val;
public ListNode next;
public ListNode(int x) {
val = x;
}
}

public static class TreeNode {
public int val;
public TreeNode left;
public TreeNode right;
public TreeNode(int x) {
val = x;
}
}

/**
* 生成连表
*
* @param str
* @return
*/
public static ListNode createList(String str){
if (str==null) return null;
String substring = str.substring(1, str.length() - 1);
if (substring.length()==0)return null;
String[] split = substring.split(",");
ListNode root = new ListNode(-1);
ListNode temp = root;
for (String s : split) {
ListNode next = new ListNode(Integer.parseInt(s));
temp.next = next;
temp = next;
}
return root.next;
}

/**
* 生成二叉树
*
* @param str
* @return
*/
public static TreeNode createTree(String str) {
if (str==null) return null;
String substring = str.substring(1, str.length() - 1);
if (substring.length()==0)return null;
String[] split = substring.split(",");
TreeNode root = split[0].equals("null")?null:new TreeNode(Integer.parseInt(split[0]));
Queue<TreeNode> queue = new LinkedBlockingQueue<>();
queue.add(root);
int index = 1;
while (!queue.isEmpty()){
TreeNode cur = queue.poll();
if (index<split.length){
cur.left = split[index].equals("null")?null:new TreeNode(Integer.parseInt(split[index]));
index++;
if (cur.left!=null){
queue.add(cur.left);
}
}else {break;}
if (index<split.length){
cur.right= split[index].equals("null")?null:new TreeNode(Integer.parseInt(split[index]));
index++;
if (cur.right!=null){
queue.add(cur.right);
}
}else {break;}
}
return root;
}

public static void show(List<TreeNode> nodes) {
nodes.forEach(node-> System.out.print(node.val + ","));
System.out.println();
}

/**
* 展示二叉树的结构
*
* @param root
*/
public static void show(TreeNode root) {
if (root == null) System.out.println("EMPTY!");
// 得到树的深度
int treeDepth = getTreeDepth(root);

// 最后一行的宽度为2的(n - 1)次方乘3,再加1
// 作为整个二维数组的宽度
int arrayHeight = treeDepth * 2 - 1;
int arrayWidth = (2 << (treeDepth - 2)) * 3 + 1;
// 用一个字符串数组来存储每个位置应显示的元素
String[][] res = new String[arrayHeight][arrayWidth];
// 对数组进行初始化,默认为一个空格
for (int i = 0; i < arrayHeight; i ++) {
for (int j = 0; j < arrayWidth; j ++) {
res[i][j] = " ";
}
}

// 从根节点开始,递归处理整个树
// res[0][(arrayWidth + 1)/ 2] = (char)(root.val + '0');
writeArray(root, 0, arrayWidth/ 2, res, treeDepth);

// 此时,已经将所有需要显示的元素储存到了二维数组中,将其拼接并打印即可
for (String[] line: res) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < line.length; i ++) {
sb.append(line[i]);
if (line[i].length() > 1 && i <= line.length - 1) {
i += line[i].length() > 4 ? 2: line[i].length() - 1;
}
}
System.out.println(sb.toString());
}
}

// 用于获得树的层数
public static int getTreeDepth(TreeNode root) {
return root == null ? 0 : (1 + Math.max(getTreeDepth(root.left), getTreeDepth(root.right)));
}


private static void writeArray(TreeNode currNode, int rowIndex, int columnIndex, String[][] res, int treeDepth) {
// 保证输入的树不为空
if (currNode == null) return;
// 先将当前节点保存到二维数组中
res[rowIndex][columnIndex] = String.valueOf(currNode.val);

// 计算当前位于树的第几层
int currLevel = ((rowIndex + 1) / 2);
// 若到了最后一层,则返回
if (currLevel == treeDepth) return;
// 计算当前行到下一行,每个元素之间的间隔(下一行的列索引与当前元素的列索引之间的间隔)
int gap = treeDepth - currLevel - 1;

// 对左儿子进行判断,若有左儿子,则记录相应的"/"与左儿子的值
if (currNode.left != null) {
res[rowIndex + 1][columnIndex - gap] = "/";
writeArray(currNode.left, rowIndex + 2, columnIndex - gap * 2, res, treeDepth);
}

// 对右儿子进行判断,若有右儿子,则记录相应的"\"与右儿子的值
if (currNode.right != null) {
res[rowIndex + 1][columnIndex + gap] = "\\";
writeArray(currNode.right, rowIndex + 2, columnIndex + gap * 2, res, treeDepth);
}
}
}

二、使用

1
2
3
4
5
// 引入TreeNode和ListNode
import com.colin.tool.LeetCode.*;

// 构造
TreeNode root = LeetCode.createTree("[1,null,2,null,3]");