Skip to content

Commit 7b5cb8c

Browse files
committed
update
1 parent c6ede9a commit 7b5cb8c

File tree

19 files changed

+834
-244
lines changed

19 files changed

+834
-244
lines changed

assets/list.eddx

0 Bytes
Binary file not shown.

codes/data-structure/src/main/java/io/github/dunwu/ds/list/LeetcodeListDemo.java

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
package io.github.dunwu.ds.list;
22

33
import java.util.ArrayList;
4-
import java.util.HashSet;
54
import java.util.List;
6-
import java.util.Set;
75

86
/**
97
* @author <a href="mailto:forbreak@163.com">Zhang Peng</a>
@@ -18,9 +16,24 @@ public class LeetcodeListDemo {
1816
*
1917
* @see <a href="https://leetcode-cn.com/problems/palindrome-linked-list/">234. 回文链表</a>
2018
*/
21-
// public static boolean isPalindrome(ListNode head) {
22-
//
23-
// }
19+
public static boolean isPalindrome(ListNode head) {
20+
List<Integer> list = new ArrayList<>();
21+
ListNode node = head;
22+
while (node != null) {
23+
list.add(node.val);
24+
node = node.next;
25+
}
26+
27+
int i = 0, j = list.size() - 1;
28+
while (i <= j) {
29+
if (!list.get(i).equals(list.get(j))) {
30+
return false;
31+
}
32+
i++;
33+
j--;
34+
}
35+
return true;
36+
}
2437

2538
/**
2639
* <code>141. 环形链表</code> 算法实现
Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
package io.github.dunwu.ds.tree;
2+
3+
import java.util.ArrayList;
4+
import java.util.LinkedList;
5+
import java.util.List;
6+
import java.util.Queue;
7+
8+
/**
9+
* 二叉树
10+
*
11+
* @author <a href="mailto:forbreak@163.com">Zhang Peng</a>
12+
* @since 2020-01-28
13+
*/
14+
public class BTree<T extends Comparable<T>> {
15+
16+
/**
17+
* 二叉树根节点
18+
*/
19+
private TreeNode<T> root;
20+
21+
public BTree() {
22+
this.root = null;
23+
}
24+
25+
public BTree(TreeNode<T> root) {
26+
this.root = root;
27+
}
28+
29+
public static <T extends Comparable<T>> BTree<T> buildTree(T... array) {
30+
BTree<T> tree = new BTree<>();
31+
List<TreeNode<T>> list = new ArrayList<>();
32+
33+
for (T value : array) {
34+
// 创建结点,每一个结点的左结点和右结点为null
35+
TreeNode<T> node;
36+
if (value == null) {
37+
node = null;
38+
} else {
39+
node = new TreeNode<>(value, null, null);
40+
}
41+
// list中存着每一个结点
42+
list.add(node);
43+
}
44+
45+
// 构建二叉树
46+
if (list.size() > 0) {
47+
// i表示的是根节点的索引,从0开始
48+
for (int i = 0; i < array.length / 2 - 1; i++) {
49+
if (list.get(2 * i + 1) != null) {
50+
// 左结点
51+
list.get(i).left = list.get(2 * i + 1);
52+
}
53+
if (list.get(2 * i + 2) != null) {
54+
// 右结点
55+
list.get(i).right = list.get(2 * i + 2);
56+
}
57+
}
58+
// 判断最后一个根结点:因为最后一个根结点可能没有右结点,所以单独拿出来处理
59+
int lastIndex = array.length / 2 - 1;
60+
// 左结点
61+
list.get(lastIndex).left = list.get(lastIndex * 2 + 1);
62+
// 右结点,如果数组的长度为奇数才有右结点
63+
if (array.length % 2 == 1) {
64+
list.get(lastIndex).right = list.get(lastIndex * 2 + 2);
65+
}
66+
67+
tree.root = list.get(0);
68+
} else {
69+
tree.root = null;
70+
}
71+
return tree;
72+
}
73+
74+
/**
75+
* 判断两颗二叉树是否完全一致
76+
* <p>
77+
* 如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的。
78+
*
79+
* @param tree1 {@link BTree}
80+
* @param tree2 {@link BTree}
81+
* @param <T> 元素类型
82+
* @return true / false
83+
*/
84+
public static <T extends Comparable<T>> boolean isEquals(final BTree<T> tree1, final BTree<T> tree2) {
85+
return isEquals(tree1.root, tree2.root);
86+
}
87+
88+
/**
89+
* 判断两颗二叉树是否完全一致
90+
*
91+
* @param root1 二叉树根节点,类型:{@link BTree#root}
92+
* @param root2 二叉树根节点,类型:{@link BTree#root}
93+
* @param <T> 元素类型
94+
* @return true / false
95+
* @see <a href="https://leetcode-cn.com/problems/same-tree/">相同的树</a>
96+
*/
97+
private static <T extends Comparable<T>> boolean isEquals(TreeNode<T> root1, TreeNode<T> root2) {
98+
if (root1 == null && root2 == null) {
99+
return true;
100+
}
101+
102+
if (root1 == null || root2 == null) {
103+
return false;
104+
}
105+
106+
if (!root1.value.equals(root2.value)) {
107+
return false;
108+
}
109+
110+
return isEquals(root1.left, root2.left) && isEquals(root1.right, root2.right);
111+
}
112+
113+
/**
114+
* 返回二叉树的最大深度
115+
*
116+
* @return 二叉树的最大深度
117+
*/
118+
public int maxDepth() {
119+
return maxDepth(this.root);
120+
}
121+
122+
/**
123+
* 采用递归方法获取二叉树的最大深度
124+
*
125+
* @param root 二叉树根节点,类型:{@link BTree#root}
126+
* @return 二叉树的最大深度
127+
* @see <a href="https://leetcode-cn.com/problems/maximum-depth-of-binary-tree/">二叉树的最大深度</a>
128+
*/
129+
private int maxDepth(TreeNode<T> root) {
130+
if (root == null) return 0;
131+
132+
int left = maxDepth(root.left);
133+
134+
int right = maxDepth(root.right);
135+
136+
return Math.max(left, right) + 1;
137+
}
138+
139+
// ------------------------------------------------------------- 遍历元素
140+
141+
/**
142+
* 将二叉树按层次遍历顺序转换为列表,即广度优先搜索(BFS)
143+
*
144+
* @return {@link List<List<T>>}
145+
* @see <a href="https://leetcode-cn.com/problems/binary-tree-level-order-traversal-ii/">二叉树的层次遍历 II</a>
146+
*/
147+
public List<List<T>> levelOrderList() {
148+
List<List<T>> lists = new ArrayList<>();
149+
if (root == null) { return lists; }
150+
Queue<TreeNode<T>> queue = new LinkedList<>();
151+
queue.offer(root);
152+
while (!queue.isEmpty()) {
153+
int size = queue.size();
154+
List<T> temp = new ArrayList<>();
155+
for (int i = 0; i < size; i++) {
156+
TreeNode<T> node = queue.poll();
157+
temp.add(node.value);
158+
if (node.left != null) { queue.offer(node.left); }
159+
if (node.right != null) { queue.offer(node.right); }
160+
}
161+
lists.add(temp);
162+
}
163+
return lists;
164+
}
165+
166+
static class TreeNode<T extends Comparable<T>> {
167+
168+
T value;
169+
170+
TreeNode<T> left;
171+
172+
TreeNode<T> right;
173+
174+
public TreeNode(T value, TreeNode<T> left, TreeNode<T> right) {
175+
this.value = value;
176+
this.left = left;
177+
this.right = right;
178+
}
179+
180+
}
181+
182+
}

codes/data-structure/src/main/java/io/github/dunwu/ds/tree/LeetcodeBTreeDemo.java

Lines changed: 0 additions & 156 deletions
This file was deleted.

codes/data-structure/src/test/java/io/github/dunwu/ds/list/LeetcodeListDemoTests.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,16 @@
99
@DisplayName("Leetcode 单链表测试例")
1010
public class LeetcodeListDemoTests {
1111

12+
@Test
13+
@DisplayName("234. 回文链表")
14+
public void isPalindromeTest() {
15+
LeetcodeListDemo.ListNode head = LeetcodeListDemo.buildList(1, 2, 2, 1);
16+
Assertions.assertTrue(LeetcodeListDemo.isPalindrome(head));
17+
18+
head = LeetcodeListDemo.buildList(1, 2);
19+
Assertions.assertFalse(LeetcodeListDemo.isPalindrome(head));
20+
}
21+
1222
@Test
1323
@DisplayName("141. 环形链表")
1424
public void hasCycleTest() {

0 commit comments

Comments
 (0)