Skip to content

修改 0054.替换数字.md Java版本解法二 和 添加 二叉树的统一迭代法.md Java版本boolean标记法 #2933

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion problems/kamacoder/0054.替换数字.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ public class Main {
String s = sc.next();
int len = s.length();
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) >= 0 && s.charAt(i) <= '9') {
if (s.charAt(i) >= '0' && s.charAt(i) <= '9') {
len += 5;
}
}
Expand Down
109 changes: 106 additions & 3 deletions problems/二叉树的统一迭代法.md
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ public:
## 其他语言版本

### Java:
迭代法前序遍历代码如下:
迭代法前序遍历(空指针标记法)代码如下:

```java
class Solution {
Expand Down Expand Up @@ -254,7 +254,42 @@ class Solution {
}
```

迭代法中序遍历代码如下:
迭代法前序遍历(boolean标记法)代码如下:

```java
class Solution {
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> res = new LinkedList<>();
if (root == null) {
return res;
}
Stack<Object[]> stack = new Stack<>();
stack.push(new Object[]{root, false});

while (!stack.empty()) {
TreeNode node = (TreeNode) stack.peek()[0];
Boolean visited = (Boolean) stack.peek()[1];
stack.pop();

if (visited) {
res.add(node.val);
} else { // 先序遍历:中左右,入栈顺序相反
if (node.right != null) {
stack.push(new Object[]{node.right, false});
}
if (node.left != null) {
stack.push(new Object[]{node.left, false});
}
stack.push(new Object[]{node, true});
}
}
return res;
}
}
```

迭代法中序遍历(空指针标记法)代码如下:

```java
class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
Expand All @@ -281,7 +316,41 @@ public List<Integer> inorderTraversal(TreeNode root) {
}
```

迭代法后序遍历代码如下:
迭代法中序遍历(boolean标记法)代码如下:

```java
class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> res = new LinkedList<>();
if (root == null) {
return res;
}
Stack<Object[]> stack = new Stack<>();
stack.push(new Object[]{root, false});
while (!stack.empty()) {
TreeNode node = (TreeNode) stack.peek()[0];
Boolean visited = (Boolean) stack.peek()[1];
stack.pop();

if (visited) {
res.add(node.val);
} else { // 中序遍历:左中右,入栈顺序相反
if (node.right != null) {
stack.push(new Object[]{node.right, false});
}
stack.push(new Object[]{node, true});
if (node.left != null) {
stack.push(new Object[]{node.left, false});
}
}
}
return res;
}
}
```

迭代法后序遍历(空指针标记法)代码如下:

```java
class Solution {
public List<Integer> postorderTraversal(TreeNode root) {
Expand Down Expand Up @@ -309,6 +378,40 @@ class Solution {
}
```

迭代法后序遍历(boolean标记法)代码如下:

```java
class Solution {
public List<Integer> postorderTraversal(TreeNode root) {
List<Integer> res = new LinkedList<>();
if (root == null) {
return res;
}
Stack<Object[]> stack = new Stack<>();
stack.push(new Object[]{root, false});

while (!stack.empty()) {
TreeNode node = (TreeNode) stack.peek()[0];
Boolean visited = (Boolean) stack.peek()[1];
stack.pop();

if (visited) {
res.add(node.val);
} else { // 后序遍历:左右中,入栈顺序相反
stack.push(new Object[]{node, true});
if (node.right != null) {
stack.push(new Object[]{node.right, false});
}
if (node.left != null) {
stack.push(new Object[]{node.left, false});
}
}
}
return res;
}
}
```

### Python:

> 迭代法前序遍历(空指针标记法):
Expand Down