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 740dce9

Browse files
committed
feat: add Binary Tree Postorder Traversal
1 parent de06bc7 commit 740dce9

File tree

3 files changed

+79
-3
lines changed

3 files changed

+79
-3
lines changed

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

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package com.hi.dhl.algorithms.leetcode._145.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>
@@ -66,4 +64,32 @@ public List<Integer> postorderTraversal(TreeNode root) {
6664
}
6765
return list;
6866
}
67+
68+
public List<Integer> postorderTraversal2(TreeNode root) {
69+
List<Integer> data = new LinkedList<>();
70+
if (root == null) return data;
71+
72+
Deque<TreeNode> stack = new ArrayDeque<>();
73+
TreeNode pre = null;
74+
while (root != null || !stack.isEmpty()) {
75+
while (true) {
76+
if (root != null) {
77+
stack.push(root);
78+
root = root.left;
79+
} else {
80+
root = stack.peek();
81+
if (root.right != null && root.right != pre) {
82+
root = root.right;
83+
} else {
84+
break;
85+
}
86+
}
87+
}
88+
89+
pre = stack.pop();
90+
data.add(pre.val);
91+
root = null;
92+
}
93+
return data;
94+
}
6995
}

‎leetcode/binary-tree/01-binary-tree-preorder.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,28 @@ class Solution {
8484
8585
return output;
8686
}
87+
88+
// 方法三
89+
public List<Integer> preorderTraversal(TreeNode root) {
90+
List<Integer> data = new LinkedList<>();
91+
if (root == null) return data;
92+
Deque<TreeNode> stack = new ArrayDeque<>();
93+
while (root != null || !stack.isEmpty()) {
94+
if (root != null) {
95+
stack.push(root);
96+
}
97+
98+
if (!stack.isEmpty()) {
99+
TreeNode node = stack.pop();
100+
data.add(node.val);
101+
if (node.right != null) {
102+
stack.push(node.right);
103+
}
104+
root = node.left;
105+
}
106+
}
107+
return data;
108+
}
87109
}
88110
```
89111

‎leetcode/binary-tree/03-binary-tree-postorder.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,34 @@ class Solution {
6666
return list;
6767
}
6868
69+
// 方法二
70+
public List<Integer> postorderTraversal2(TreeNode root) {
71+
List<Integer> data = new LinkedList<>();
72+
if (root == null) return data;
73+
74+
Deque<TreeNode> stack = new ArrayDeque<>();
75+
TreeNode pre = null;
76+
while (root != null || !stack.isEmpty()) {
77+
while (true) {
78+
if (root != null) {
79+
stack.push(root);
80+
root = root.left;
81+
} else {
82+
root = stack.peek();
83+
if (root.right != null && root.right != pre) {
84+
root = root.right;
85+
} else {
86+
break;
87+
}
88+
}
89+
}
90+
91+
pre = stack.pop();
92+
data.add(pre.val);
93+
root = null;
94+
}
95+
return data;
96+
}
6997
}
7098
```
7199

0 commit comments

Comments
(0)

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