mirror of
https://github.com/krahets/hello-algo.git
synced 2024-12-24 11:06:29 +08:00
Format the Java codes with the Reat Hat extension.
This commit is contained in:
parent
7273ee24e8
commit
f8513455b5
39 changed files with 195 additions and 205 deletions
3
.gitignore
vendored
3
.gitignore
vendored
|
@ -13,3 +13,6 @@ docs/overrides/
|
|||
build/
|
||||
site/
|
||||
utils/
|
||||
|
||||
# test script
|
||||
test.sh
|
||||
|
|
|
@ -13,8 +13,7 @@ public class array {
|
|||
/* 随机返回一个数组元素 */
|
||||
static int randomAccess(int[] nums) {
|
||||
// 在区间 [0, nums.length) 中随机抽取一个数字
|
||||
int randomIndex = ThreadLocalRandom.current().
|
||||
nextInt(0, nums.length);
|
||||
int randomIndex = ThreadLocalRandom.current().nextInt(0, nums.length);
|
||||
// 获取并返回随机元素
|
||||
int randomNum = nums[randomIndex];
|
||||
return randomNum;
|
||||
|
@ -79,15 +78,15 @@ public class array {
|
|||
System.out.println("数组 arr = " + Arrays.toString(arr));
|
||||
int[] nums = { 1, 3, 2, 5, 4 };
|
||||
System.out.println("数组 nums = " + Arrays.toString(nums));
|
||||
|
||||
|
||||
/* 随机访问 */
|
||||
int randomNum = randomAccess(nums);
|
||||
System.out.println("在 nums 中获取随机元素 " + randomNum);
|
||||
|
||||
|
||||
/* 长度扩展 */
|
||||
nums = extend(nums, 3);
|
||||
System.out.println("将数组长度扩展至 8 ,得到 nums = " + Arrays.toString(nums));
|
||||
|
||||
|
||||
/* 插入元素 */
|
||||
insert(nums, 6, 3);
|
||||
System.out.println("在索引 3 处插入数字 6 ,得到 nums = " + Arrays.toString(nums));
|
||||
|
@ -95,10 +94,10 @@ public class array {
|
|||
/* 删除元素 */
|
||||
remove(nums, 2);
|
||||
System.out.println("删除索引 2 处的元素,得到 nums = " + Arrays.toString(nums));
|
||||
|
||||
|
||||
/* 遍历数组 */
|
||||
traverse(nums);
|
||||
|
||||
|
||||
/* 查找元素 */
|
||||
int index = find(nums, 3);
|
||||
System.out.println("在 nums 中查找元素 3 ,得到索引 = " + index);
|
||||
|
|
|
@ -51,7 +51,7 @@ public class linked_list {
|
|||
/* Driver Code */
|
||||
public static void main(String[] args) {
|
||||
/* 初始化链表 */
|
||||
// 初始化各个节点
|
||||
// 初始化各个节点
|
||||
ListNode n0 = new ListNode(1);
|
||||
ListNode n1 = new ListNode(3);
|
||||
ListNode n2 = new ListNode(2);
|
||||
|
|
|
@ -10,17 +10,17 @@ import java.util.*;
|
|||
|
||||
/* 列表类简易实现 */
|
||||
class MyList {
|
||||
private int[] nums; // 数组(存储列表元素)
|
||||
private int capacity = 10; // 列表容量
|
||||
private int size = 0; // 列表长度(即当前元素数量)
|
||||
private int extendRatio = 2; // 每次列表扩容的倍数
|
||||
private int[] nums; // 数组(存储列表元素)
|
||||
private int capacity = 10; // 列表容量
|
||||
private int size = 0; // 列表长度(即当前元素数量)
|
||||
private int extendRatio = 2; // 每次列表扩容的倍数
|
||||
|
||||
/* 构造方法 */
|
||||
public MyList() {
|
||||
nums = new int[capacity];
|
||||
}
|
||||
|
||||
/* 获取列表长度(即当前元素数量)*/
|
||||
/* 获取列表长度(即当前元素数量) */
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
@ -118,7 +118,7 @@ public class my_list {
|
|||
list.add(5);
|
||||
list.add(4);
|
||||
System.out.println("列表 list = " + Arrays.toString(list.toArray()) +
|
||||
" ,容量 = " + list.capacity() + " ,长度 = " + list.size());
|
||||
" ,容量 = " + list.capacity() + " ,长度 = " + list.size());
|
||||
|
||||
/* 中间插入元素 */
|
||||
list.insert(3, 6);
|
||||
|
@ -142,6 +142,6 @@ public class my_list {
|
|||
list.add(i);
|
||||
}
|
||||
System.out.println("扩容后的列表 list = " + Arrays.toString(list.toArray()) +
|
||||
" ,容量 = " + list.capacity() + " ,长度 = " + list.size());
|
||||
" ,容量 = " + list.capacity() + " ,长度 = " + list.size());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,7 +8,6 @@ package chapter_computational_complexity;
|
|||
|
||||
import java.util.*;
|
||||
|
||||
|
||||
public class leetcode_two_sum {
|
||||
/* 方法一:暴力枚举 */
|
||||
static int[] twoSumBruteForce(int[] nums, int target) {
|
||||
|
@ -40,9 +39,9 @@ public class leetcode_two_sum {
|
|||
|
||||
public static void main(String[] args) {
|
||||
// ======= Test Case =======
|
||||
int[] nums = { 2,7,11,15 };
|
||||
int[] nums = { 2, 7, 11, 15 };
|
||||
int target = 9;
|
||||
|
||||
|
||||
// ====== Driver Code ======
|
||||
// 方法一
|
||||
int[] res = twoSumBruteForce(nums, target);
|
||||
|
|
|
@ -15,7 +15,7 @@ public class space_complexity {
|
|||
// do something
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/* 常数阶 */
|
||||
static void constant(int n) {
|
||||
// 常量、变量、对象占用 O(1) 空间
|
||||
|
@ -52,7 +52,8 @@ public class space_complexity {
|
|||
/* 线性阶(递归实现) */
|
||||
static void linearRecur(int n) {
|
||||
System.out.println("递归 n = " + n);
|
||||
if (n == 1) return;
|
||||
if (n == 1)
|
||||
return;
|
||||
linearRecur(n - 1);
|
||||
}
|
||||
|
||||
|
@ -73,7 +74,8 @@ public class space_complexity {
|
|||
|
||||
/* 平方阶(递归实现) */
|
||||
static int quadraticRecur(int n) {
|
||||
if (n <= 0) return 0;
|
||||
if (n <= 0)
|
||||
return 0;
|
||||
// 数组 nums 长度为 n, n-1, ..., 2, 1
|
||||
int[] nums = new int[n];
|
||||
System.out.println("递归 n = " + n + " 中的 nums 长度 = " + nums.length);
|
||||
|
@ -82,7 +84,8 @@ public class space_complexity {
|
|||
|
||||
/* 指数阶(建立满二叉树) */
|
||||
static TreeNode buildTree(int n) {
|
||||
if (n == 0) return null;
|
||||
if (n == 0)
|
||||
return null;
|
||||
TreeNode root = new TreeNode(0);
|
||||
root.left = buildTree(n - 1);
|
||||
root.right = buildTree(n - 1);
|
||||
|
|
|
@ -23,7 +23,7 @@ public class time_complexity {
|
|||
count++;
|
||||
return count;
|
||||
}
|
||||
|
||||
|
||||
/* 线性阶(遍历数组) */
|
||||
static int arrayTraversal(int[] nums) {
|
||||
int count = 0;
|
||||
|
@ -48,7 +48,7 @@ public class time_complexity {
|
|||
|
||||
/* 平方阶(冒泡排序) */
|
||||
static int bubbleSort(int[] nums) {
|
||||
int count = 0; // 计数器
|
||||
int count = 0; // 计数器
|
||||
// 外循环:待排序元素数量为 n-1, n-2, ..., 1
|
||||
for (int i = nums.length - 1; i > 0; i--) {
|
||||
// 内循环:冒泡操作
|
||||
|
@ -58,7 +58,7 @@ public class time_complexity {
|
|||
int tmp = nums[j];
|
||||
nums[j] = nums[j + 1];
|
||||
nums[j + 1] = tmp;
|
||||
count += 3; // 元素交换包含 3 个单元操作
|
||||
count += 3; // 元素交换包含 3 个单元操作
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -81,7 +81,8 @@ public class time_complexity {
|
|||
|
||||
/* 指数阶(递归实现) */
|
||||
static int expRecur(int n) {
|
||||
if (n == 1) return 1;
|
||||
if (n == 1)
|
||||
return 1;
|
||||
return expRecur(n - 1) + expRecur(n - 1) + 1;
|
||||
}
|
||||
|
||||
|
@ -97,15 +98,17 @@ public class time_complexity {
|
|||
|
||||
/* 对数阶(递归实现) */
|
||||
static int logRecur(float n) {
|
||||
if (n <= 1) return 0;
|
||||
if (n <= 1)
|
||||
return 0;
|
||||
return logRecur(n / 2) + 1;
|
||||
}
|
||||
|
||||
/* 线性对数阶 */
|
||||
static int linearLogRecur(float n) {
|
||||
if (n <= 1) return 1;
|
||||
int count = linearLogRecur(n / 2) +
|
||||
linearLogRecur(n / 2);
|
||||
if (n <= 1)
|
||||
return 1;
|
||||
int count = linearLogRecur(n / 2) +
|
||||
linearLogRecur(n / 2);
|
||||
for (int i = 0; i < n; i++) {
|
||||
count++;
|
||||
}
|
||||
|
@ -114,7 +117,8 @@ public class time_complexity {
|
|||
|
||||
/* 阶乘阶(递归实现) */
|
||||
static int factorialRecur(int n) {
|
||||
if (n == 0) return 1;
|
||||
if (n == 0)
|
||||
return 1;
|
||||
int count = 0;
|
||||
// 从 1 个分裂出 n 个
|
||||
for (int i = 0; i < n; i++) {
|
||||
|
@ -141,7 +145,7 @@ public class time_complexity {
|
|||
System.out.println("平方阶的计算操作数量 = " + count);
|
||||
int[] nums = new int[n];
|
||||
for (int i = 0; i < n; i++)
|
||||
nums[i] = n - i; // [n,n-1,...,2,1]
|
||||
nums[i] = n - i; // [n,n-1,...,2,1]
|
||||
count = bubbleSort(nums);
|
||||
System.out.println("平方阶(冒泡排序)的计算操作数量 = " + count);
|
||||
|
||||
|
|
|
@ -36,7 +36,7 @@ public class worst_best_time_complexity {
|
|||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
/* Driver Code */
|
||||
public static void main(String[] args) {
|
||||
for (int i = 0; i < 10; i++) {
|
||||
|
|
|
@ -84,7 +84,7 @@ public class graph_adjacency_list {
|
|||
public static void main(String[] args) {
|
||||
/* 初始化无向图 */
|
||||
Vertex[] v = Vertex.valsToVets(new int[] { 1, 3, 2, 5, 4 });
|
||||
Vertex[][] edges = { { v[0], v[1] }, { v[0], v[3] }, { v[1], v[2] },
|
||||
Vertex[][] edges = { { v[0], v[1] }, { v[0], v[3] }, { v[1], v[2] },
|
||||
{ v[2], v[3] }, { v[2], v[4] }, { v[3], v[4] } };
|
||||
GraphAdjList graph = new GraphAdjList(edges);
|
||||
System.out.println("\n初始化后,图为");
|
||||
|
|
|
@ -11,7 +11,7 @@ import java.util.*;
|
|||
|
||||
/* 基于邻接矩阵实现的无向图类 */
|
||||
class GraphAdjMat {
|
||||
List<Integer> vertices; // 顶点列表,元素代表“顶点值”,索引代表“顶点索引”
|
||||
List<Integer> vertices; // 顶点列表,元素代表“顶点值”,索引代表“顶点索引”
|
||||
List<List<Integer>> adjMat; // 邻接矩阵,行列索引对应“顶点索引”
|
||||
|
||||
/* 构造方法 */
|
||||
|
|
|
@ -38,7 +38,7 @@ public class graph_bfs {
|
|||
public static void main(String[] args) {
|
||||
/* 初始化无向图 */
|
||||
Vertex[] v = Vertex.valsToVets(new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });
|
||||
Vertex[][] edges = { { v[0], v[1] }, { v[0], v[3] }, { v[1], v[2] }, { v[1], v[4] },
|
||||
Vertex[][] edges = { { v[0], v[1] }, { v[0], v[3] }, { v[1], v[2] }, { v[1], v[4] },
|
||||
{ v[2], v[5] }, { v[3], v[4] }, { v[3], v[6] }, { v[4], v[5] },
|
||||
{ v[4], v[7] }, { v[5], v[8] }, { v[6], v[7] }, { v[7], v[8] } };
|
||||
GraphAdjList graph = new GraphAdjList(edges);
|
||||
|
|
|
@ -37,7 +37,7 @@ public class graph_dfs {
|
|||
public static void main(String[] args) {
|
||||
/* 初始化无向图 */
|
||||
Vertex[] v = Vertex.valsToVets(new int[] { 0, 1, 2, 3, 4, 5, 6 });
|
||||
Vertex[][] edges = { { v[0], v[1] }, { v[0], v[3] }, { v[1], v[2] },
|
||||
Vertex[][] edges = { { v[0], v[1] }, { v[0], v[3] }, { v[1], v[2] },
|
||||
{ v[2], v[5] }, { v[4], v[5] }, { v[5], v[6] } };
|
||||
GraphAdjList graph = new GraphAdjList(edges);
|
||||
System.out.println("\n初始化后,图为");
|
||||
|
|
|
@ -12,6 +12,7 @@ import java.util.*;
|
|||
class Entry {
|
||||
public int key;
|
||||
public String val;
|
||||
|
||||
public Entry(int key, String val) {
|
||||
this.key = key;
|
||||
this.val = val;
|
||||
|
@ -21,6 +22,7 @@ class Entry {
|
|||
/* 基于数组简易实现的哈希表 */
|
||||
class ArrayHashMap {
|
||||
private List<Entry> buckets;
|
||||
|
||||
public ArrayHashMap() {
|
||||
// 初始化数组,包含 100 个桶
|
||||
buckets = new ArrayList<>();
|
||||
|
@ -39,7 +41,8 @@ class ArrayHashMap {
|
|||
public String get(int key) {
|
||||
int index = hashFunc(key);
|
||||
Entry pair = buckets.get(index);
|
||||
if (pair == null) return null;
|
||||
if (pair == null)
|
||||
return null;
|
||||
return pair.val;
|
||||
}
|
||||
|
||||
|
@ -89,13 +92,12 @@ class ArrayHashMap {
|
|||
|
||||
/* 打印哈希表 */
|
||||
public void print() {
|
||||
for (Entry kv: entrySet()) {
|
||||
for (Entry kv : entrySet()) {
|
||||
System.out.println(kv.key + " -> " + kv.val);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public class array_hash_map {
|
||||
public static void main(String[] args) {
|
||||
/* 初始化哈希表 */
|
||||
|
@ -103,9 +105,9 @@ public class array_hash_map {
|
|||
|
||||
/* 添加操作 */
|
||||
// 在哈希表中添加键值对 (key, value)
|
||||
map.put(12836, "小哈");
|
||||
map.put(15937, "小啰");
|
||||
map.put(16750, "小算");
|
||||
map.put(12836, "小哈");
|
||||
map.put(15937, "小啰");
|
||||
map.put(16750, "小算");
|
||||
map.put(13276, "小法");
|
||||
map.put(10583, "小鸭");
|
||||
System.out.println("\n添加完成后,哈希表为\nKey -> Value");
|
||||
|
@ -124,15 +126,15 @@ public class array_hash_map {
|
|||
|
||||
/* 遍历哈希表 */
|
||||
System.out.println("\n遍历键值对 Key->Value");
|
||||
for (Entry kv: map.entrySet()) {
|
||||
for (Entry kv : map.entrySet()) {
|
||||
System.out.println(kv.key + " -> " + kv.val);
|
||||
}
|
||||
System.out.println("\n单独遍历键 Key");
|
||||
for (int key: map.keySet()) {
|
||||
for (int key : map.keySet()) {
|
||||
System.out.println(key);
|
||||
}
|
||||
System.out.println("\n单独遍历值 Value");
|
||||
for (String val: map.valueSet()) {
|
||||
for (String val : map.valueSet()) {
|
||||
System.out.println(val);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -16,9 +16,9 @@ public class hash_map {
|
|||
|
||||
/* 添加操作 */
|
||||
// 在哈希表中添加键值对 (key, value)
|
||||
map.put(12836, "小哈");
|
||||
map.put(15937, "小啰");
|
||||
map.put(16750, "小算");
|
||||
map.put(12836, "小哈");
|
||||
map.put(15937, "小啰");
|
||||
map.put(16750, "小算");
|
||||
map.put(13276, "小法");
|
||||
map.put(10583, "小鸭");
|
||||
System.out.println("\n添加完成后,哈希表为\nKey -> Value");
|
||||
|
@ -37,15 +37,15 @@ public class hash_map {
|
|||
|
||||
/* 遍历哈希表 */
|
||||
System.out.println("\n遍历键值对 Key->Value");
|
||||
for (Map.Entry <Integer, String> kv: map.entrySet()) {
|
||||
for (Map.Entry<Integer, String> kv : map.entrySet()) {
|
||||
System.out.println(kv.getKey() + " -> " + kv.getValue());
|
||||
}
|
||||
System.out.println("\n单独遍历键 Key");
|
||||
for (int key: map.keySet()) {
|
||||
for (int key : map.keySet()) {
|
||||
System.out.println(key);
|
||||
}
|
||||
System.out.println("\n单独遍历值 Value");
|
||||
for (String val: map.values()) {
|
||||
for (String val : map.values()) {
|
||||
System.out.println(val);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -9,7 +9,6 @@ package chapter_heap;
|
|||
import include.*;
|
||||
import java.util.*;
|
||||
|
||||
|
||||
public class heap {
|
||||
public static void testPush(Queue<Integer> heap, int val) {
|
||||
heap.offer(val); // 元素入堆
|
||||
|
|
|
@ -41,9 +41,9 @@ class MaxHeap {
|
|||
|
||||
/* 交换元素 */
|
||||
private void swap(int i, int j) {
|
||||
int a = maxHeap.get(i),
|
||||
b = maxHeap.get(j),
|
||||
tmp = a;
|
||||
int a = maxHeap.get(i);
|
||||
int b = maxHeap.get(j);
|
||||
int tmp = a;
|
||||
maxHeap.set(i, b);
|
||||
maxHeap.set(j, tmp);
|
||||
}
|
||||
|
@ -111,7 +111,8 @@ class MaxHeap {
|
|||
if (r < size() && maxHeap.get(r) > maxHeap.get(ma))
|
||||
ma = r;
|
||||
// 若节点 i 最大或索引 l, r 越界,则无需继续堆化,跳出
|
||||
if (ma == i) break;
|
||||
if (ma == i)
|
||||
break;
|
||||
// 交换两节点
|
||||
swap(i, ma);
|
||||
// 循环向下堆化
|
||||
|
@ -127,7 +128,6 @@ class MaxHeap {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
public class my_heap {
|
||||
public static void main(String[] args) {
|
||||
/* 初始化大顶堆 */
|
||||
|
|
|
@ -42,11 +42,11 @@ public class binary_search {
|
|||
// 未找到目标元素,返回 -1
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
int target = 6;
|
||||
int[] nums = { 1, 3, 6, 8, 12, 15, 23, 67, 70, 92 };
|
||||
|
||||
|
||||
/* 二分查找(双闭区间) */
|
||||
int index = binarySearch(nums, target);
|
||||
System.out.println("目标元素 6 的索引 = " + index);
|
||||
|
|
|
@ -32,7 +32,7 @@ public class hashing_search {
|
|||
// 初始化哈希表
|
||||
Map<Integer, Integer> map = new HashMap<>();
|
||||
for (int i = 0; i < nums.length; i++) {
|
||||
map.put(nums[i], i); // key: 元素,value: 索引
|
||||
map.put(nums[i], i); // key: 元素,value: 索引
|
||||
}
|
||||
int index = hashingSearchArray(map, target);
|
||||
System.out.println("目标元素 3 的索引 = " + index);
|
||||
|
@ -42,7 +42,7 @@ public class hashing_search {
|
|||
// 初始化哈希表
|
||||
Map<Integer, ListNode> map1 = new HashMap<>();
|
||||
while (head != null) {
|
||||
map1.put(head.val, head); // key: 节点值,value: 节点
|
||||
map1.put(head.val, head); // key: 节点值,value: 节点
|
||||
head = head.next;
|
||||
}
|
||||
ListNode node = hashingSearchLinkedList(map1, target);
|
||||
|
|
|
@ -25,7 +25,7 @@ public class bubble_sort {
|
|||
}
|
||||
}
|
||||
|
||||
/* 冒泡排序(标志优化)*/
|
||||
/* 冒泡排序(标志优化) */
|
||||
static void bubbleSortWithFlag(int[] nums) {
|
||||
// 外循环:待排序元素数量为 n-1, n-2, ..., 1
|
||||
for (int i = nums.length - 1; i > 0; i--) {
|
||||
|
@ -37,10 +37,11 @@ public class bubble_sort {
|
|||
int tmp = nums[j];
|
||||
nums[j] = nums[j + 1];
|
||||
nums[j + 1] = tmp;
|
||||
flag = true; // 记录交换元素
|
||||
flag = true; // 记录交换元素
|
||||
}
|
||||
}
|
||||
if (!flag) break; // 此轮冒泡未交换任何元素,直接跳出
|
||||
if (!flag)
|
||||
break; // 此轮冒泡未交换任何元素,直接跳出
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -70,7 +70,7 @@ public class counting_sort {
|
|||
int[] nums = { 1, 0, 1, 2, 0, 4, 0, 2, 2, 4 };
|
||||
countingSortNaive(nums);
|
||||
System.out.println("计数排序(无法排序对象)完成后 nums = " + Arrays.toString(nums));
|
||||
|
||||
|
||||
int[] nums1 = { 1, 0, 1, 2, 0, 4, 0, 2, 2, 4 };
|
||||
countingSort(nums1);
|
||||
System.out.println("计数排序完成后 nums1 = " + Arrays.toString(nums1));
|
||||
|
|
|
@ -16,10 +16,10 @@ public class insertion_sort {
|
|||
int base = nums[i], j = i - 1;
|
||||
// 内循环:将 base 插入到左边的正确位置
|
||||
while (j >= 0 && nums[j] > base) {
|
||||
nums[j + 1] = nums[j]; // 1. 将 nums[j] 向右移动一位
|
||||
nums[j + 1] = nums[j]; // 1. 将 nums[j] 向右移动一位
|
||||
j--;
|
||||
}
|
||||
nums[j + 1] = base; // 2. 将 base 赋值到正确位置
|
||||
nums[j + 1] = base; // 2. 将 base 赋值到正确位置
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -14,13 +14,13 @@ public class merge_sort {
|
|||
// 右子数组区间 [mid + 1, right]
|
||||
static void merge(int[] nums, int left, int mid, int right) {
|
||||
// 初始化辅助数组
|
||||
int[] tmp = Arrays.copyOfRange(nums, left, right + 1);
|
||||
// 左子数组的起始索引和结束索引
|
||||
int[] tmp = Arrays.copyOfRange(nums, left, right + 1);
|
||||
// 左子数组的起始索引和结束索引
|
||||
int leftStart = left - left, leftEnd = mid - left;
|
||||
// 右子数组的起始索引和结束索引
|
||||
// 右子数组的起始索引和结束索引
|
||||
int rightStart = mid + 1 - left, rightEnd = right - left;
|
||||
// i, j 分别指向左子数组、右子数组的首元素
|
||||
int i = leftStart, j = rightStart;
|
||||
int i = leftStart, j = rightStart;
|
||||
// 通过覆盖原数组 nums 来合并左子数组和右子数组
|
||||
for (int k = left; k <= right; k++) {
|
||||
// 若“左子数组已全部合并完”,则选取右子数组元素,并且 j++
|
||||
|
@ -38,7 +38,8 @@ public class merge_sort {
|
|||
/* 归并排序 */
|
||||
static void mergeSort(int[] nums, int left, int right) {
|
||||
// 终止条件
|
||||
if (left >= right) return; // 当子数组长度为 1 时终止递归
|
||||
if (left >= right)
|
||||
return; // 当子数组长度为 1 时终止递归
|
||||
// 划分阶段
|
||||
int mid = (left + right) / 2; // 计算中点
|
||||
mergeSort(nums, left, mid); // 递归左子数组
|
||||
|
|
|
@ -119,7 +119,7 @@ class QuickSortTailCall {
|
|||
swap(nums, i, j); // 交换这两个元素
|
||||
}
|
||||
swap(nums, i, left); // 将基准数交换至两子数组的分界线
|
||||
return i; // 返回基准数的索引
|
||||
return i; // 返回基准数的索引
|
||||
}
|
||||
|
||||
/* 快速排序(尾递归优化) */
|
||||
|
@ -130,8 +130,8 @@ class QuickSortTailCall {
|
|||
int pivot = partition(nums, left, right);
|
||||
// 对两个子数组中较短的那个执行快排
|
||||
if (pivot - left < right - pivot) {
|
||||
quickSort(nums, left, pivot - 1); // 递归排序左子数组
|
||||
left = pivot + 1; // 剩余待排序区间为 [pivot + 1, right]
|
||||
quickSort(nums, left, pivot - 1); // 递归排序左子数组
|
||||
left = pivot + 1; // 剩余待排序区间为 [pivot + 1, right]
|
||||
} else {
|
||||
quickSort(nums, pivot + 1, right); // 递归排序右子数组
|
||||
right = pivot - 1; // 剩余待排序区间为 [left, pivot - 1]
|
||||
|
|
|
@ -47,7 +47,8 @@ public class radix_sort {
|
|||
// 获取数组的最大元素,用于判断最大位数
|
||||
int m = Integer.MIN_VALUE;
|
||||
for (int num : nums)
|
||||
if (num > m) m = num;
|
||||
if (num > m)
|
||||
m = num;
|
||||
// 按照从低位到高位的顺序遍历
|
||||
for (int exp = 1; exp <= m; exp *= 10)
|
||||
// 对数组元素的第 k 位执行计数排序
|
||||
|
@ -59,7 +60,7 @@ public class radix_sort {
|
|||
|
||||
public static void main(String[] args) {
|
||||
// 基数排序
|
||||
int[] nums = { 10546151, 35663510, 42865989, 34862445, 81883077,
|
||||
int[] nums = { 10546151, 35663510, 42865989, 34862445, 81883077,
|
||||
88906420, 72429244, 30524779, 82060337, 63832996 };
|
||||
radixSort(nums);
|
||||
System.out.println("基数排序完成后 nums = " + Arrays.toString(nums));
|
||||
|
|
|
@ -10,8 +10,8 @@ import java.util.*;
|
|||
|
||||
/* 基于环形数组实现的双向队列 */
|
||||
class ArrayDeque {
|
||||
private int[] nums; // 用于存储双向队列元素的数组
|
||||
private int front; // 队首指针,指向队首元素
|
||||
private int[] nums; // 用于存储双向队列元素的数组
|
||||
private int front; // 队首指针,指向队首元素
|
||||
private int queSize; // 双向队列长度
|
||||
|
||||
/* 构造方法 */
|
||||
|
|
|
@ -10,8 +10,8 @@ import java.util.*;
|
|||
|
||||
/* 基于环形数组实现的队列 */
|
||||
class ArrayQueue {
|
||||
private int[] nums; // 用于存储队列元素的数组
|
||||
private int front; // 队首指针,指向队首元素
|
||||
private int[] nums; // 用于存储队列元素的数组
|
||||
private int front; // 队首指针,指向队首元素
|
||||
private int queSize; // 队列长度
|
||||
|
||||
public ArrayQueue(int capacity) {
|
||||
|
|
|
@ -11,6 +11,7 @@ import java.util.*;
|
|||
/* 基于数组实现的栈 */
|
||||
class ArrayStack {
|
||||
private ArrayList<Integer> stack;
|
||||
|
||||
public ArrayStack() {
|
||||
// 初始化列表(动态数组)
|
||||
stack = new ArrayList<>();
|
||||
|
|
|
@ -10,9 +10,10 @@ import java.util.*;
|
|||
|
||||
/* 双向链表节点 */
|
||||
class ListNode {
|
||||
int val; // 节点值
|
||||
int val; // 节点值
|
||||
ListNode next; // 后继节点引用(指针)
|
||||
ListNode prev; // 前驱节点引用(指针)
|
||||
|
||||
ListNode(int val) {
|
||||
this.val = val;
|
||||
prev = next = null;
|
||||
|
@ -22,7 +23,7 @@ class ListNode {
|
|||
/* 基于双向链表实现的双向队列 */
|
||||
class LinkedListDeque {
|
||||
private ListNode front, rear; // 头节点 front ,尾节点 rear
|
||||
private int queSize = 0; // 双向队列的长度
|
||||
private int queSize = 0; // 双向队列的长度
|
||||
|
||||
public LinkedListDeque() {
|
||||
front = rear = null;
|
||||
|
@ -55,7 +56,7 @@ class LinkedListDeque {
|
|||
// 将 node 添加至链表尾部
|
||||
rear.next = node;
|
||||
node.prev = rear;
|
||||
rear = node; // 更新尾节点
|
||||
rear = node; // 更新尾节点
|
||||
}
|
||||
queSize++; // 更新队列长度
|
||||
}
|
||||
|
@ -85,17 +86,17 @@ class LinkedListDeque {
|
|||
fNext.prev = null;
|
||||
front.next = null;
|
||||
}
|
||||
front = fNext; // 更新头节点
|
||||
front = fNext; // 更新头节点
|
||||
// 队尾出队操作
|
||||
} else {
|
||||
val = rear.val; // 暂存尾节点值
|
||||
val = rear.val; // 暂存尾节点值
|
||||
// 删除尾节点
|
||||
ListNode rPrev = rear.prev;
|
||||
if (rPrev != null) {
|
||||
rPrev.next = null;
|
||||
rear.prev = null;
|
||||
}
|
||||
rear = rPrev; // 更新尾节点
|
||||
rear = rPrev; // 更新尾节点
|
||||
}
|
||||
queSize--; // 更新队列长度
|
||||
return val;
|
||||
|
|
|
@ -10,7 +10,7 @@ import java.util.*;
|
|||
|
||||
/* 基于链表实现的队列 */
|
||||
class LinkedListQueue {
|
||||
private ListNode front, rear; // 头节点 front ,尾节点 rear
|
||||
private ListNode front, rear; // 头节点 front ,尾节点 rear
|
||||
private int queSize = 0;
|
||||
|
||||
public LinkedListQueue() {
|
||||
|
|
|
@ -11,9 +11,9 @@ import include.*;
|
|||
|
||||
/* 基于链表实现的栈 */
|
||||
class LinkedListStack {
|
||||
private ListNode stackPeek; // 将头节点作为栈顶
|
||||
private int stkSize = 0; // 栈的长度
|
||||
|
||||
private ListNode stackPeek; // 将头节点作为栈顶
|
||||
private int stkSize = 0; // 栈的长度
|
||||
|
||||
public LinkedListStack() {
|
||||
stackPeek = null;
|
||||
}
|
||||
|
|
|
@ -27,7 +27,8 @@ class AVLTree {
|
|||
/* 获取平衡因子 */
|
||||
public int balanceFactor(TreeNode node) {
|
||||
// 空节点平衡因子为 0
|
||||
if (node == null) return 0;
|
||||
if (node == null)
|
||||
return 0;
|
||||
// 节点平衡因子 = 左子树高度 - 右子树高度
|
||||
return height(node.left) - height(node.right);
|
||||
}
|
||||
|
@ -98,15 +99,16 @@ class AVLTree {
|
|||
|
||||
/* 递归插入节点(辅助方法) */
|
||||
private TreeNode insertHelper(TreeNode node, int val) {
|
||||
if (node == null) return new TreeNode(val);
|
||||
if (node == null)
|
||||
return new TreeNode(val);
|
||||
/* 1. 查找插入位置,并插入节点 */
|
||||
if (val < node.val)
|
||||
node.left = insertHelper(node.left, val);
|
||||
else if (val > node.val)
|
||||
node.right = insertHelper(node.right, val);
|
||||
else
|
||||
return node; // 重复节点不插入,直接返回
|
||||
updateHeight(node); // 更新节点高度
|
||||
return node; // 重复节点不插入,直接返回
|
||||
updateHeight(node); // 更新节点高度
|
||||
/* 2. 执行旋转操作,使该子树重新恢复平衡 */
|
||||
node = rotate(node);
|
||||
// 返回子树的根节点
|
||||
|
@ -121,7 +123,8 @@ class AVLTree {
|
|||
|
||||
/* 递归删除节点(辅助方法) */
|
||||
private TreeNode removeHelper(TreeNode node, int val) {
|
||||
if (node == null) return null;
|
||||
if (node == null)
|
||||
return null;
|
||||
/* 1. 查找节点,并删除之 */
|
||||
if (val < node.val)
|
||||
node.left = removeHelper(node.left, val);
|
||||
|
@ -143,7 +146,7 @@ class AVLTree {
|
|||
node.val = temp.val;
|
||||
}
|
||||
}
|
||||
updateHeight(node); // 更新节点高度
|
||||
updateHeight(node); // 更新节点高度
|
||||
/* 2. 执行旋转操作,使该子树重新恢复平衡 */
|
||||
node = rotate(node);
|
||||
// 返回子树的根节点
|
||||
|
@ -152,7 +155,8 @@ class AVLTree {
|
|||
|
||||
/* 获取中序遍历中的下一个节点(仅适用于 root 有左子节点的情况) */
|
||||
private TreeNode getInOrderNext(TreeNode node) {
|
||||
if (node == null) return node;
|
||||
if (node == null)
|
||||
return node;
|
||||
// 循环访问左子节点,直到叶节点时为最小节点,跳出
|
||||
while (node.left != null) {
|
||||
node = node.left;
|
||||
|
|
|
@ -15,7 +15,7 @@ class BinarySearchTree {
|
|||
|
||||
public BinarySearchTree(int[] nums) {
|
||||
Arrays.sort(nums); // 排序数组
|
||||
root = buildTree(nums, 0, nums.length - 1); // 构建二叉搜索树
|
||||
root = buildTree(nums, 0, nums.length - 1); // 构建二叉搜索树
|
||||
}
|
||||
|
||||
/* 获取二叉树根节点 */
|
||||
|
@ -25,7 +25,8 @@ class BinarySearchTree {
|
|||
|
||||
/* 构建二叉搜索树 */
|
||||
public TreeNode buildTree(int[] nums, int i, int j) {
|
||||
if (i > j) return null;
|
||||
if (i > j)
|
||||
return null;
|
||||
// 将数组中间节点作为根节点
|
||||
int mid = (i + j) / 2;
|
||||
TreeNode root = new TreeNode(nums[mid]);
|
||||
|
@ -41,11 +42,14 @@ class BinarySearchTree {
|
|||
// 循环查找,越过叶节点后跳出
|
||||
while (cur != null) {
|
||||
// 目标节点在 cur 的右子树中
|
||||
if (cur.val < num) cur = cur.right;
|
||||
if (cur.val < num)
|
||||
cur = cur.right;
|
||||
// 目标节点在 cur 的左子树中
|
||||
else if (cur.val > num) cur = cur.left;
|
||||
else if (cur.val > num)
|
||||
cur = cur.left;
|
||||
// 找到目标节点,跳出循环
|
||||
else break;
|
||||
else
|
||||
break;
|
||||
}
|
||||
// 返回目标节点
|
||||
return cur;
|
||||
|
@ -54,49 +58,62 @@ class BinarySearchTree {
|
|||
/* 插入节点 */
|
||||
public TreeNode insert(int num) {
|
||||
// 若树为空,直接提前返回
|
||||
if (root == null) return null;
|
||||
if (root == null)
|
||||
return null;
|
||||
TreeNode cur = root, pre = null;
|
||||
// 循环查找,越过叶节点后跳出
|
||||
while (cur != null) {
|
||||
// 找到重复节点,直接返回
|
||||
if (cur.val == num) return null;
|
||||
if (cur.val == num)
|
||||
return null;
|
||||
pre = cur;
|
||||
// 插入位置在 cur 的右子树中
|
||||
if (cur.val < num) cur = cur.right;
|
||||
if (cur.val < num)
|
||||
cur = cur.right;
|
||||
// 插入位置在 cur 的左子树中
|
||||
else cur = cur.left;
|
||||
else
|
||||
cur = cur.left;
|
||||
}
|
||||
// 插入节点 val
|
||||
TreeNode node = new TreeNode(num);
|
||||
if (pre.val < num) pre.right = node;
|
||||
else pre.left = node;
|
||||
if (pre.val < num)
|
||||
pre.right = node;
|
||||
else
|
||||
pre.left = node;
|
||||
return node;
|
||||
}
|
||||
|
||||
/* 删除节点 */
|
||||
public TreeNode remove(int num) {
|
||||
// 若树为空,直接提前返回
|
||||
if (root == null) return null;
|
||||
if (root == null)
|
||||
return null;
|
||||
TreeNode cur = root, pre = null;
|
||||
// 循环查找,越过叶节点后跳出
|
||||
while (cur != null) {
|
||||
// 找到待删除节点,跳出循环
|
||||
if (cur.val == num) break;
|
||||
if (cur.val == num)
|
||||
break;
|
||||
pre = cur;
|
||||
// 待删除节点在 cur 的右子树中
|
||||
if (cur.val < num) cur = cur.right;
|
||||
if (cur.val < num)
|
||||
cur = cur.right;
|
||||
// 待删除节点在 cur 的左子树中
|
||||
else cur = cur.left;
|
||||
else
|
||||
cur = cur.left;
|
||||
}
|
||||
// 若无待删除节点,则直接返回
|
||||
if (cur == null) return null;
|
||||
if (cur == null)
|
||||
return null;
|
||||
// 子节点数量 = 0 or 1
|
||||
if (cur.left == null || cur.right == null) {
|
||||
// 当子节点数量 = 0 / 1 时, child = null / 该子节点
|
||||
TreeNode child = cur.left != null ? cur.left : cur.right;
|
||||
// 删除节点 cur
|
||||
if (pre.left == cur) pre.left = child;
|
||||
else pre.right = child;
|
||||
if (pre.left == cur)
|
||||
pre.left = child;
|
||||
else
|
||||
pre.right = child;
|
||||
}
|
||||
// 子节点数量 = 2
|
||||
else {
|
||||
|
@ -113,7 +130,8 @@ class BinarySearchTree {
|
|||
|
||||
/* 获取中序遍历中的下一个节点(仅适用于 root 有左子节点的情况) */
|
||||
public TreeNode getInOrderNext(TreeNode root) {
|
||||
if (root == null) return root;
|
||||
if (root == null)
|
||||
return root;
|
||||
// 循环访问左子节点,直到叶节点时为最小节点,跳出
|
||||
while (root.left != null) {
|
||||
root = root.left;
|
||||
|
|
|
@ -17,12 +17,12 @@ public class binary_tree_bfs {
|
|||
// 初始化一个列表,用于保存遍历序列
|
||||
List<Integer> list = new ArrayList<>();
|
||||
while (!queue.isEmpty()) {
|
||||
TreeNode node = queue.poll(); // 队列出队
|
||||
list.add(node.val); // 保存节点值
|
||||
TreeNode node = queue.poll(); // 队列出队
|
||||
list.add(node.val); // 保存节点值
|
||||
if (node.left != null)
|
||||
queue.offer(node.left); // 左子节点入队
|
||||
queue.offer(node.left); // 左子节点入队
|
||||
if (node.right != null)
|
||||
queue.offer(node.right); // 右子节点入队
|
||||
queue.offer(node.right); // 右子节点入队
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
|
|
@ -15,7 +15,8 @@ public class binary_tree_dfs {
|
|||
|
||||
/* 前序遍历 */
|
||||
static void preOrder(TreeNode root) {
|
||||
if (root == null) return;
|
||||
if (root == null)
|
||||
return;
|
||||
// 访问优先级:根节点 -> 左子树 -> 右子树
|
||||
list.add(root.val);
|
||||
preOrder(root.left);
|
||||
|
@ -24,7 +25,8 @@ public class binary_tree_dfs {
|
|||
|
||||
/* 中序遍历 */
|
||||
static void inOrder(TreeNode root) {
|
||||
if (root == null) return;
|
||||
if (root == null)
|
||||
return;
|
||||
// 访问优先级:左子树 -> 根节点 -> 右子树
|
||||
inOrder(root.left);
|
||||
list.add(root.val);
|
||||
|
@ -33,7 +35,8 @@ public class binary_tree_dfs {
|
|||
|
||||
/* 后序遍历 */
|
||||
static void postOrder(TreeNode root) {
|
||||
if (root == null) return;
|
||||
if (root == null)
|
||||
return;
|
||||
// 访问优先级:左子树 -> 右子树 -> 根节点
|
||||
postOrder(root.left);
|
||||
postOrder(root.right);
|
||||
|
|
|
@ -6,9 +6,7 @@
|
|||
|
||||
package include;
|
||||
|
||||
/**
|
||||
* Definition for a singly-linked list node
|
||||
*/
|
||||
/* Definition for a singly-linked list node */
|
||||
public class ListNode {
|
||||
public int val;
|
||||
public ListNode next;
|
||||
|
@ -16,12 +14,8 @@ public class ListNode {
|
|||
public ListNode(int x) {
|
||||
val = x;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a linked list with an array
|
||||
* @param arr
|
||||
* @return
|
||||
*/
|
||||
|
||||
/* Generate a linked list with an array */
|
||||
public static ListNode arrToLinkedList(int[] arr) {
|
||||
ListNode dum = new ListNode(0);
|
||||
ListNode head = dum;
|
||||
|
@ -32,12 +26,7 @@ public class ListNode {
|
|||
return dum.next;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a list node with specific value from a linked list
|
||||
* @param head
|
||||
* @param val
|
||||
* @return
|
||||
*/
|
||||
/* Get a list node with specific value from a linked list */
|
||||
public static ListNode getListNode(ListNode head, int val) {
|
||||
while (head != null && head.val != val) {
|
||||
head = head.next;
|
||||
|
|
|
@ -8,7 +8,6 @@ package include;
|
|||
|
||||
import java.util.*;
|
||||
|
||||
|
||||
class Trunk {
|
||||
Trunk prev;
|
||||
String str;
|
||||
|
@ -21,11 +20,7 @@ class Trunk {
|
|||
|
||||
public class PrintUtil {
|
||||
|
||||
/**
|
||||
* Print a matrix (Array)
|
||||
* @param <T>
|
||||
* @param matrix
|
||||
*/
|
||||
/* Print a matrix (Array) */
|
||||
public static <T> void printMatrix(T[][] matrix) {
|
||||
System.out.println("[");
|
||||
for (T[] row : matrix) {
|
||||
|
@ -34,11 +29,7 @@ public class PrintUtil {
|
|||
System.out.println("]");
|
||||
}
|
||||
|
||||
/**
|
||||
* Print a matrix (List)
|
||||
* @param <T>
|
||||
* @param matrix
|
||||
*/
|
||||
/* Print a matrix (List) */
|
||||
public static <T> void printMatrix(List<List<T>> matrix) {
|
||||
System.out.println("[");
|
||||
for (List<T> row : matrix) {
|
||||
|
@ -47,10 +38,7 @@ public class PrintUtil {
|
|||
System.out.println("]");
|
||||
}
|
||||
|
||||
/**
|
||||
* Print a linked list
|
||||
* @param head
|
||||
*/
|
||||
/* Print a linked list */
|
||||
public static void printLinkedList(ListNode head) {
|
||||
List<String> list = new ArrayList<>();
|
||||
while (head != null) {
|
||||
|
@ -64,18 +52,12 @@ public class PrintUtil {
|
|||
* The interface of the tree printer
|
||||
* This tree printer is borrowed from TECHIE DELIGHT
|
||||
* https://www.techiedelight.com/c-program-print-binary-tree/
|
||||
* @param root
|
||||
*/
|
||||
public static void printTree(TreeNode root) {
|
||||
printTree(root, null, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Print a binary tree
|
||||
* @param root
|
||||
* @param prev
|
||||
* @param isLeft
|
||||
*/
|
||||
/* Print a binary tree */
|
||||
public static void printTree(TreeNode root, Trunk prev, boolean isLeft) {
|
||||
if (root == null) {
|
||||
return;
|
||||
|
@ -107,10 +89,7 @@ public class PrintUtil {
|
|||
printTree(root.left, trunk, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function to print branches of the binary tree
|
||||
* @param p
|
||||
*/
|
||||
/* Helper function to print branches of the binary tree */
|
||||
public static void showTrunks(Trunk p) {
|
||||
if (p == null) {
|
||||
return;
|
||||
|
@ -120,22 +99,14 @@ public class PrintUtil {
|
|||
System.out.print(p.str);
|
||||
}
|
||||
|
||||
/**
|
||||
* Print a hash map
|
||||
* @param <K>
|
||||
* @param <V>
|
||||
* @param map
|
||||
*/
|
||||
/* Print a hash map */
|
||||
public static <K, V> void printHashMap(Map<K, V> map) {
|
||||
for (Map.Entry <K, V> kv: map.entrySet()) {
|
||||
for (Map.Entry<K, V> kv : map.entrySet()) {
|
||||
System.out.println(kv.getKey() + " -> " + kv.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Print a heap (PriorityQueue)
|
||||
* @param queue
|
||||
*/
|
||||
/* Print a heap (PriorityQueue) */
|
||||
public static void printHeap(Queue<Integer> queue) {
|
||||
List<Integer> list = new ArrayList<>(queue);
|
||||
System.out.print("堆的数组表示:");
|
||||
|
|
|
@ -8,40 +8,36 @@ package include;
|
|||
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* Definition for a binary tree node.
|
||||
*/
|
||||
/* Definition for a binary tree node. */
|
||||
public class TreeNode {
|
||||
public int val; // 节点值
|
||||
public int height; // 节点高度
|
||||
public TreeNode left; // 左子节点引用
|
||||
public int val; // 节点值
|
||||
public int height; // 节点高度
|
||||
public TreeNode left; // 左子节点引用
|
||||
public TreeNode right; // 右子节点引用
|
||||
|
||||
public TreeNode(int x) {
|
||||
val = x;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a binary tree given an array
|
||||
* @param list
|
||||
* @return
|
||||
*/
|
||||
/* Generate a binary tree given an array */
|
||||
public static TreeNode listToTree(List<Integer> list) {
|
||||
int size = list.size();
|
||||
if (size == 0)
|
||||
return null;
|
||||
|
||||
|
||||
TreeNode root = new TreeNode(list.get(0));
|
||||
Queue<TreeNode> queue = new LinkedList<>() {{ add(root); }};
|
||||
int i = 0;
|
||||
while(!queue.isEmpty()) {
|
||||
while (!queue.isEmpty()) {
|
||||
TreeNode node = queue.poll();
|
||||
if (++i >= size) break;
|
||||
if (++i >= size)
|
||||
break;
|
||||
if (list.get(i) != null) {
|
||||
node.left = new TreeNode(list.get(i));
|
||||
queue.add(node.left);
|
||||
}
|
||||
if (++i >= size) break;
|
||||
if (++i >= size)
|
||||
break;
|
||||
if (list.get(i) != null) {
|
||||
node.right = new TreeNode(list.get(i));
|
||||
queue.add(node.right);
|
||||
|
@ -50,23 +46,19 @@ public class TreeNode {
|
|||
return root;
|
||||
}
|
||||
|
||||
/**
|
||||
* Serialize a binary tree to a list
|
||||
* @param root
|
||||
* @return
|
||||
*/
|
||||
/* Serialize a binary tree to a list */
|
||||
public static List<Integer> treeToList(TreeNode root) {
|
||||
List<Integer> list = new ArrayList<>();
|
||||
if(root == null) return list;
|
||||
if (root == null)
|
||||
return list;
|
||||
Queue<TreeNode> queue = new LinkedList<>() {{ add(root); }};
|
||||
while(!queue.isEmpty()) {
|
||||
while (!queue.isEmpty()) {
|
||||
TreeNode node = queue.poll();
|
||||
if(node != null) {
|
||||
if (node != null) {
|
||||
list.add(node.val);
|
||||
queue.add(node.left);
|
||||
queue.add(node.right);
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
list.add(null);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -11,6 +11,7 @@ import java.util.*;
|
|||
/* 顶点类 */
|
||||
public class Vertex {
|
||||
public int val;
|
||||
|
||||
public Vertex(int val) {
|
||||
this.val = val;
|
||||
}
|
||||
|
@ -32,4 +33,4 @@ public class Vertex {
|
|||
}
|
||||
return vals;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
2
codes/python/.gitignore
vendored
2
codes/python/.gitignore
vendored
|
@ -1,3 +1 @@
|
|||
__pycache__
|
||||
|
||||
test_all.sh
|
||||
|
|
Loading…
Reference in a new issue