Skip to content

Commit 5e4ecde

Browse files
committed
update problem tags
1 parent 3bb9d83 commit 5e4ecde

File tree

2 files changed

+65
-47
lines changed

2 files changed

+65
-47
lines changed

zh-hans/binary_tree/binary_tree_inorder_traversal.md

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -247,16 +247,17 @@ public:
247247
public class Solution {
248248
public List<Integer> inorderTraversal(TreeNode root) {
249249
List<Integer> result = new ArrayList<Integer>();
250-
251250
Deque<TreeNode> stack = new ArrayDeque<TreeNode>();
252-
while (root != null || (!stack.isEmpty())) {
253-
if (root != null) {
254-
stack.push(root);
255-
root = root.left;
251+
252+
TreeNode curr = root;
253+
while (curr != null || !stack.isEmpty()) {
254+
if (curr != null) {
255+
stack.push(curr);
256+
curr = curr.left;
256257
} else {
257-
root = stack.pop();
258-
result.add(root.val);
259-
root = root.right;
258+
curr = stack.pop();
259+
result.add(curr.val);
260+
curr = curr.right;
260261
}
261262
}
262263

@@ -267,7 +268,7 @@ public class Solution {
267268

268269
### 源码分析
269270

270-
使用栈的思想模拟递归,注意迭代的演进和边界条件即可。
271+
使用栈的思想模拟递归,注意迭代的演进和边界条件即可。Java 中新建变量 curr 而不是复用 root 观察下来有一点性能提升。
271272

272273
### 复杂度分析
273274

zh-hans/binary_tree/binary_tree_postorder_traversal.md

Lines changed: 55 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,32 @@
11
# Binary Tree Postorder Traversal
22

3+
Tags: Tree, Stack, Hard
4+
35
## Question
46

5-
- leetcode: [Binary Tree Postorder Traversal | LeetCode OJ](https://leetcode.com/problems/binary-tree-postorder-traversal/)
6-
- lintcode: [(68) Binary Tree Postorder Traversal](http://www.lintcode.com/en/problem/binary-tree-postorder-traversal/)
7+
- leetcode: [Binary Tree Postorder Traversal](https://leetcode.com/problems/binary-tree-postorder-traversal/)
8+
- lintcode: [Binary Tree Postorder Traversal](http://www.lintcode.com/en/problem/binary-tree-postorder-traversal/)
79

810
### Problem Statement
911

1012
Given a binary tree, return the _postorder_ traversal of its nodes' values.
1113

12-
#### Example
13-
14-
Given binary tree `{1,#,2,3}`,
15-
16-
14+
For example:
15+
Given binary tree `{1,#,2,3}`,
1716

17+
18+
19+
1820
1
1921
\
2022
2
2123
/
2224
3
23-
25+
2426

2527
return `[3,2,1]`.
2628

27-
#### Challenge
28-
29-
Can you do it without recursion?
29+
**Note:** Recursive solution is trivial, could you do it iteratively?
3030

3131

3232
## 题解1 - 递归
@@ -124,6 +124,32 @@ public class Solution {
124124
}
125125
```
126126

127+
### Java - Traversal
128+
129+
```java
130+
/**
131+
* Definition for a binary tree node.
132+
* public class TreeNode {
133+
* int val;
134+
* TreeNode left;
135+
* TreeNode right;
136+
* TreeNode(int x) { val = x; }
137+
* }
138+
*/
139+
public class Solution {
140+
private List<Integer> result = new ArrayList<Integer>();
141+
142+
public List<Integer> postorderTraversal(TreeNode root) {
143+
if (root != null) {
144+
postorderTraversal(root.left);
145+
postorderTraversal(root.right);
146+
result.add(root.val);
147+
}
148+
return result;
149+
}
150+
}
151+
```
152+
127153
### 源码分析
128154

129155
递归版的太简单了,没啥好说的,注意入栈顺序。
@@ -238,10 +264,9 @@ public:
238264
public class Solution {
239265
public List<Integer> postorderTraversal(TreeNode root) {
240266
List<Integer> result = new ArrayList<Integer>();
241-
if (root == null) return result;
242-
243267
Deque<TreeNode> stack = new ArrayDeque<TreeNode>();
244-
stack.push(root);
268+
269+
if (root != null) stack.push(root);
245270
TreeNode prev = null;
246271
while (!stack.isEmpty()) {
247272
TreeNode curr = stack.peek();
@@ -250,10 +275,9 @@ public class Solution {
250275
if (prev != null && (curr.left == prev || curr.right == prev)) {
251276
childVisited = true;
252277
}
253-
254278
if (noChild || childVisited) {
279+
curr = stack.pop();
255280
result.add(curr.val);
256-
stack.pop();
257281
prev = curr;
258282
} else {
259283
if (curr.right != null) stack.push(curr.right);
@@ -268,7 +292,7 @@ public class Solution {
268292

269293
### 源码分析
270294

271-
遍历顺序为『左右根』,判断根节点是否应该从栈中剔除有两种条件,一为无子节点,二为子节点已遍历过。判断子节点是否遍历过需要排除`prev == null` 的情况,因为 prev 初始化为 null.
295+
遍历顺序为『左右根』,判断根节点是否应该从栈中剔除有两种条件,一为无子节点,二为子节点已遍历过。判断子节点是否遍历过需要排除`prev == null` 的情况,因为 prev 初始化为 null. Java 中在 while 内部新建 curr 变量而不是复用 root 有一定性能提升,不知是不是 JVM 运行时优化导致的。
272296

273297
**将递归写成迭代的难点在于如何在迭代中体现递归本质及边界条件的确立,可使用简单示例和纸上画出栈调用图辅助分析。**
274298

@@ -326,35 +350,28 @@ public:
326350
327351
```java
328352
/**
329-
* Definition of TreeNode:
353+
* Definition for a binary tree node.
330354
* public class TreeNode {
331-
* public int val;
332-
* public TreeNode left, right;
333-
* public TreeNode(int val) {
334-
* this.val = val;
335-
* this.left = this.right = null;
336-
* }
355+
* int val;
356+
* TreeNode left;
357+
* TreeNode right;
358+
* TreeNode(int x) { val = x; }
337359
* }
338360
*/
339361
public class Solution {
340-
/**
341-
* @param root: The root of binary tree.
342-
* @return: Postorder in ArrayList which contains node values.
343-
*/
344-
public ArrayList<Integer> postorderTraversal(TreeNode root) {
345-
ArrayList<Integer> result = new ArrayList<Integer>();
346-
if (root == null) return result;
347-
362+
public List<Integer> postorderTraversal(TreeNode root) {
363+
List<Integer> result = new ArrayList<Integer>();
348364
Deque<TreeNode> stack = new ArrayDeque<TreeNode>();
349-
stack.push(root);
365+
if (root != null) stack.push(root);
366+
350367
while (!stack.isEmpty()) {
351-
TreeNode node = stack.pop();
352-
result.add(node.val);
353-
if (node.left != null) stack.push(node.left);
354-
if (node.right != null) stack.push(node.right);
368+
TreeNode curr = stack.pop();
369+
result.add(curr.val);
370+
if (curr.left != null) stack.push(curr.left);
371+
if (curr.right != null) stack.push(curr.right);
355372
}
356-
Collections.reverse(result);
357373
374+
Collections.reverse(result);
358375
return result;
359376
}
360377
}

0 commit comments

Comments
 (0)