Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit de06bc7

Browse files
committed
feat: add Binary Tree Preorder Traversal
1 parent 65c97dd commit de06bc7

File tree

2 files changed

+35
-10
lines changed

2 files changed

+35
-10
lines changed

‎00-code(源代码)/src/com/hi/dhl/algorithms/leetcode/_144/java/Solution.java‎

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package com.hi.dhl.algorithms.leetcode._144.java;
22

3-
import java.util.LinkedList;
4-
import java.util.List;
5-
import java.util.Stack;
3+
import java.util.*;
64

75
/**
86
* <pre>
@@ -83,4 +81,25 @@ public List<Integer> preorderTraversal(TreeNode root) {
8381
return list;
8482
}
8583

84+
// 方法二
85+
public List<Integer> preorderTraversal3(TreeNode root) {
86+
List<Integer> data = new LinkedList<>();
87+
if (root == null) return data;
88+
Deque<TreeNode> stack = new ArrayDeque<>();
89+
while (root != null || !stack.isEmpty()) {
90+
if (root != null) {
91+
stack.push(root);
92+
}
93+
94+
if (!stack.isEmpty()) {
95+
TreeNode node = stack.pop();
96+
data.add(node.val);
97+
if (node.right != null) {
98+
stack.push(node.right);
99+
}
100+
root = node.left;
101+
}
102+
}
103+
return data;
104+
}
86105
}

‎offer/algorithm/19-zheng-ze-biao-da-shi-pi-pei.md‎

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,9 @@ class Solution {
162162
fun isMatch(s: String, p: String): Boolean {
163163
val row = s.length
164164
val colum = p.length
165-
val dp = Array(row + 1) { BooleanArray(colum + 1) }
165+
val dp = Array(row + 1) { BooleanArray(colum + 1) }
166+
167+
// 处理空字符串的情况
166168
dp[0][0] = true;
167169
for (i in 1..colum) {
168170
if (p[i - 1] == '*' && dp[0][i - 2]) {
@@ -174,16 +176,19 @@ class Solution {
174176
for (j in 1..colum) {
175177
val ms = s[i - 1]
176178
val mp = p[j - 1]
179+
// s=abc p=a.c 或者 s=abc p=abc 当第 i 个字符相等,看前 i-1个字符是否相等
177180
if (mp == ms || mp == '.') {
178181
dp[i][j] = dp[i - 1][j - 1]
179182
} else if (mp == '*') {
180183
if (j < 2) continue
181-
184+
// s=abc p=abc* 匹配到一个字符,需要去掉 p 中最后一个字符即 dp[i][j - 1]
185+
// s=abccc p= abc* 匹配到多个字符,需要去掉 s 中最后一个字符,即 dp[i - 1][j]
182186
val mpLast = p[j - 2]
183187
if (ms == mpLast || mpLast == '.') {
184188
dp[i][j] = dp[i - 1][j] || dp[i][j - 1]
185189
}
186190

191+
// s=abc p=abcd* 字符不相等 或者 s=abc p=abcc* 匹配到 0 个字符,需要去掉 p 中最后链两个字符串
187192
dp[i][j] = dp[i][j] || dp[i][j - 2]
188193
}
189194
}
@@ -205,6 +210,8 @@ class Solution {
205210
int row = s.length();
206211
int colum = p.length();
207212
boolean[][] dp = new boolean[row + 1][colum + 1];
213+
214+
// 处理空字符串的情况
208215
dp[0][0] = true;
209216
for (int j = 1; j <= colum; j++) {
210217
if (p.charAt(j - 1) == '*' && dp[0][j - 2]) {
@@ -217,24 +224,23 @@ class Solution {
217224
char ms = s.charAt(i - 1);
218225
char mp = p.charAt(j - 1);
219226

220-
/**
221-
* 两种情况 * 和 非*
222-
*/
227+
// s=abc p=a.c 或者 s=abc p=abc 当第 i 个字符相等,看前 i-1个字符是否相等
223228
if (ms == mp || mp == '.') {
224-
// 非*
225229
dp[i][j] = dp[i - 1][j - 1];
226230

227231
} else if (mp == '*') {
228232
// 遇到 * 号,则代码 P[m−2]=c 可以重复0次或多次,它们是一个整体 c*
229233

230234
if (j < 2) continue;
231235

236+
// s=abc p=abc* 匹配到一个字符,需要去掉 p 中最后一个字符即 dp[i][j - 1]
237+
// s=abccc p= abc* 匹配到多个字符,需要去掉 s 中最后一个字符,即 dp[i - 1][j]
232238
char mpLast = p.charAt(j - 2);
233239
if (mpLast == ms || mpLast == '.') {
234240
dp[i][j] = dp[i - 1][j] || dp[i][j - 1];
235241
}
236242

237-
// P[n−1] 是 0 个 c,P 最后两个字符废了
243+
// s=abc p=abcd* 字符不相等 或者 s=abc p=abcc* 匹配到 0 个字符,需要去掉 p 中最后链两个字符串
238244
dp[i][j] = dp[i][j] || dp[i][j - 2];
239245

240246
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /