Skip to content

Commit 3d18d48

Browse files
committed
Chapter 06 Optional 03 new postorder non-recursive traversal algo added.
1 parent b171fb6 commit 3d18d48

File tree

9 files changed

+213
-110
lines changed

9 files changed

+213
-110
lines changed
Lines changed: 18 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/// Source : https://leetcode.com/problems/binary-tree-postorder-traversal/description/
22
/// Author : liuyubobobo
3-
/// Time : 2018-05-31
3+
/// Time : 2018-05-30
44

55
#include <iostream>
66
#include <vector>
@@ -18,10 +18,10 @@ struct TreeNode {
1818

1919

2020
// Non-Recursive
21-
// Using a pre pointer to record the last visted node
21+
// Using two stacks, Reverse Preorder Traversal!
2222
//
2323
// Time Complexity: O(n)
24-
// Space Complexity: O(h)
24+
// Space Complexity: O(n)
2525
class Solution {
2626

2727
public:
@@ -31,48 +31,33 @@ class Solution {
3131
if(root == NULL)
3232
return res;
3333

34-
stack<TreeNode*> stack;
35-
TreeNode* pre = NULL;
34+
stack<TreeNode*> stack, output;
3635

37-
stack.push(root);
38-
while(!stack.empty()){
39-
40-
TreeNode* node = stack.top();
41-
stack.pop();
42-
if((node->left == NULL && node->right == NULL) ||
43-
(pre != NULL && pre == node->left && node->right == NULL) ||
44-
(pre != NULL && pre == node->right)){
45-
res.push_back(node->val);
46-
pre = node;
36+
TreeNode* p = root;
37+
while(p != NULL || !stack.empty()){
38+
if(p != NULL){
39+
stack.push(p);
40+
output.push(p);
41+
p = p->right;
4742
}
4843
else{
49-
stack.push(node);
50-
51-
if(node->right != NULL)
52-
stack.push(node->right);
53-
if(node->left != NULL)
54-
stack.push(node->left);
44+
p = stack.top();
45+
stack.pop();
46+
p = p->left;
5547
}
5648
}
5749

50+
while(!output.empty()){
51+
res.push_back((output.top())->val);
52+
output.pop();
53+
}
54+
5855
return res;
5956
}
6057
};
6158

62-
63-
void print_vec(const vector<int>& vec){
64-
for(int e: vec)
65-
cout << e << " ";
66-
cout << endl;
67-
}
68-
6959
int main() {
7060

71-
TreeNode* root = new TreeNode(1);
72-
root->right = new TreeNode(2);
73-
root->right->left = new TreeNode(3);
74-
print_vec(Solution().postorderTraversal(root));
75-
7661
return 0;
7762
}
7863

06-Stack-and-Queue/Course Code (C++)/Optional-03-Classic-Non-Recursive-Postorder-Traversal/main4.cpp

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ struct TreeNode {
1717
};
1818

1919

20-
// Classic Non-Recursive
21-
// Using a pre pointer to record the last visted node
20+
// Non-Recursive
21+
// Using a pre pointer to record the last visited node
2222
//
2323
// Time Complexity: O(n)
2424
// Space Complexity: O(h)
@@ -33,26 +33,25 @@ class Solution {
3333

3434
stack<TreeNode*> stack;
3535
TreeNode* pre = NULL;
36-
TreeNode* cur = root;
3736

38-
while(cur != NULL || !stack.empty()){
37+
stack.push(root);
38+
while(!stack.empty()){
3939

40-
while(cur != NULL){
41-
stack.push(cur);
42-
cur = cur->left;
43-
}
44-
45-
cur = stack.top();
40+
TreeNode* node = stack.top();
4641
stack.pop();
47-
48-
if(cur->right == NULL || pre == cur->right){
49-
res.push_back(cur->val);
50-
pre = cur;
51-
cur = NULL;
42+
if((node->left == NULL && node->right == NULL) ||
43+
(pre != NULL && pre == node->left && node->right == NULL) ||
44+
(pre != NULL && pre == node->right)){
45+
res.push_back(node->val);
46+
pre = node;
5247
}
5348
else{
54-
stack.push(cur);
55-
cur = cur->right;
49+
stack.push(node);
50+
51+
if(node->right != NULL)
52+
stack.push(node->right);
53+
if(node->left != NULL)
54+
stack.push(node->left);
5655
}
5756
}
5857

06-Stack-and-Queue/Course Code (C++)/Optional-03-Classic-Non-Recursive-Postorder-Traversal/main5.cpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,23 +36,23 @@ class Solution {
3636
TreeNode* cur = root;
3737

3838
while(cur != NULL || !stack.empty()){
39-
if(cur != NULL){
39+
40+
while(cur != NULL){
4041
stack.push(cur);
4142
cur = cur->left;
4243
}
44+
45+
cur = stack.top();
46+
stack.pop();
47+
48+
if(cur->right == NULL || pre == cur->right){
49+
res.push_back(cur->val);
50+
pre = cur;
51+
cur = NULL;
52+
}
4353
else{
44-
cur = stack.top();
45-
stack.pop();
46-
47-
if(cur->right == NULL || pre == cur->right){
48-
res.push_back(cur->val);
49-
pre = cur;
50-
cur = NULL;
51-
}
52-
else{
53-
stack.push(cur);
54-
cur = cur->right;
55-
}
54+
stack.push(cur);
55+
cur = cur->right;
5656
}
5757
}
5858

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/// Source : https://leetcode.com/problems/binary-tree-postorder-traversal/description/
2+
/// Author : liuyubobobo
3+
/// Time : 2018-05-31
4+
5+
#include <iostream>
6+
#include <vector>
7+
#include <stack>
8+
9+
using namespace std;
10+
11+
/// Definition for a binary tree node.
12+
struct TreeNode {
13+
int val;
14+
TreeNode *left;
15+
TreeNode *right;
16+
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
17+
};
18+
19+
20+
// Classic Non-Recursive
21+
// Using a pre pointer to record the last visted node
22+
//
23+
// Time Complexity: O(n)
24+
// Space Complexity: O(h)
25+
class Solution {
26+
27+
public:
28+
vector<int> postorderTraversal(TreeNode* root) {
29+
30+
vector<int> res;
31+
if(root == NULL)
32+
return res;
33+
34+
stack<TreeNode*> stack;
35+
TreeNode* pre = NULL;
36+
TreeNode* cur = root;
37+
38+
while(cur != NULL || !stack.empty()){
39+
if(cur != NULL){
40+
stack.push(cur);
41+
cur = cur->left;
42+
}
43+
else{
44+
cur = stack.top();
45+
stack.pop();
46+
47+
if(cur->right == NULL || pre == cur->right){
48+
res.push_back(cur->val);
49+
pre = cur;
50+
cur = NULL;
51+
}
52+
else{
53+
stack.push(cur);
54+
cur = cur->right;
55+
}
56+
}
57+
}
58+
59+
return res;
60+
}
61+
};
62+
63+
64+
void print_vec(const vector<int>& vec){
65+
for(int e: vec)
66+
cout << e << " ";
67+
cout << endl;
68+
}
69+
70+
int main() {
71+
72+
TreeNode* root = new TreeNode(1);
73+
root->right = new TreeNode(2);
74+
root->right->left = new TreeNode(3);
75+
print_vec(Solution().postorderTraversal(root));
76+
77+
return 0;
78+
}
79+

06-Stack-and-Queue/Course Code (Java)/Optional-03-Classic-Non-Recursive-Postorder-Traversal/src/Solution2.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import java.util.Stack;
88

99
// Non-Recursive
10-
// Using two stacks
10+
// Using two stacks, Reverse Preorder Traversal!
1111
//
1212
// Time Complexity: O(n)
1313
// Space Complexity: O(n)
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,40 @@
11
/// Source : https://leetcode.com/problems/binary-tree-postorder-traversal/description/
22
/// Author : liuyubobobo
3-
/// Time : 2018-05-31
3+
/// Time : 2018-07-03
44

55
import java.util.ArrayList;
66
import java.util.List;
77
import java.util.Stack;
8+
import java.util.LinkedList;
89

910
// Non-Recursive
10-
// Using a pre pointer to record the last visted node
11+
// Using two stacks, Reverse Preorder Traversal!
1112
//
1213
// Time Complexity: O(n)
13-
// Space Complexity: O(h)
14+
// Space Complexity: O(n)
1415
public class Solution3 {
1516

16-
public List<Integer> postorderTraversal(TreeNode root) {
17-
18-
ArrayList<Integer> res = new ArrayList<Integer>();
19-
if(root == null)
20-
return res;
17+
public List<Integer> postorderTraversal(TreeNode root){
2118

2219
Stack<TreeNode> stack = new Stack<>();
23-
TreeNode pre = null;
24-
25-
stack.push(root);
26-
while(!stack.empty()){
20+
LinkedList<TreeNode> output = new LinkedList<>();
2721

28-
TreeNode cur = stack.pop();
29-
if((cur.left == null && cur.right == null) ||
30-
(pre != null && pre == cur.left && cur.right == null) ||
31-
(pre != null && pre == cur.right)){
32-
res.add(cur.val);
33-
pre = cur;
22+
TreeNode p = root;
23+
while(p != null || !stack.isEmpty()){
24+
if(p != null){
25+
stack.push(p);
26+
output.push(p);
27+
p = p.right;
3428
}
3529
else{
36-
stack.push(cur);
37-
if(cur.right != null)
38-
stack.push(cur.right);
39-
if(cur.left != null)
40-
stack.push(cur.left);
30+
p = stack.pop();
31+
p = p.left;
4132
}
4233
}
34+
35+
ArrayList<Integer> res = new ArrayList<>();
36+
while(!output.isEmpty())
37+
res.add(output.pop().val);
4338
return res;
4439
}
4540
}

06-Stack-and-Queue/Course Code (Java)/Optional-03-Classic-Non-Recursive-Postorder-Traversal/src/Solution4.java

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import java.util.List;
77
import java.util.Stack;
88

9-
// Classic Non-Recursive
9+
// Non-Recursive
1010
// Using a pre pointer to record the last visted node
1111
//
1212
// Time Complexity: O(n)
@@ -21,24 +21,23 @@ public List<Integer> postorderTraversal(TreeNode root) {
2121

2222
Stack<TreeNode> stack = new Stack<>();
2323
TreeNode pre = null;
24-
TreeNode cur = root;
2524

26-
while(cur != null || !stack.empty()){
25+
stack.push(root);
26+
while(!stack.empty()){
2727

28-
while(cur != null){
29-
stack.push(cur);
30-
cur = cur.left;
31-
}
32-
33-
cur = stack.pop();
34-
if(cur.right == null || pre == cur.right){
28+
TreeNode cur = stack.pop();
29+
if((cur.left == null && cur.right == null) ||
30+
(pre != null && pre == cur.left && cur.right == null) ||
31+
(pre != null && pre == cur.right)){
3532
res.add(cur.val);
3633
pre = cur;
37-
cur = null;
3834
}
3935
else{
4036
stack.push(cur);
41-
cur = cur.right;
37+
if(cur.right != null)
38+
stack.push(cur.right);
39+
if(cur.left != null)
40+
stack.push(cur.left);
4241
}
4342
}
4443
return res;

0 commit comments

Comments
 (0)