-
Notifications
You must be signed in to change notification settings - Fork 0
/
Binary Tree Postorder Traversal.cpp
129 lines (114 loc) · 3.3 KB
/
Binary Tree Postorder Traversal.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/**
* Given a binary tree, return the postorder traversal of its nodes' values.
*
* For example:
* Given binary tree {1,#,2,3},
* 1
* \
* 2
* /
* 3
* return [3,2,1].
*
* Note: Recursive solution is trivial, could you do it iteratively?
*/
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
// recursive solution
class Solution {
void do_postorder(TreeNode *root, vector<int> &ret) {
if (root->left == nullptr && root->right == nullptr) {
ret.push_back(root->val);
return;
}
if (root->left != nullptr) {
do_postorder(root->left, ret);
}
if (root->right != nullptr) {
do_postorder(root->right, ret);
}
ret.push_back(root->val);
}
public:
vector<int> postorderTraversal(TreeNode *root) {
vector<int> ret;
if (root == nullptr) {
return ret;
}
do_postorder(root, ret);
return ret;
}
};
// iterative solution: updated on 2014/09/30
class Solution {
public:
vector<int> postorderTraversal(TreeNode *root) {
vector<int> result;
if (root == nullptr) return result;
stack<TreeNode *> stk;
TreeNode *pre = nullptr, *cur = root;
stk.push(root);
while (!stk.empty()) {
cur = stk.top();
if (pre == nullptr || pre->left == cur || pre->right == cur) {
if (cur->left != nullptr) {
stk.push(cur->left);
} else if (cur->right != nullptr) {
stk.push(cur->right);
} else {
result.push_back(cur->val);
stk.pop();
}
} else if (cur->left == pre) {
if (cur->right != nullptr) {
stk.push(cur->right);
} else {
result.push_back(cur->val);
stk.pop();
}
} else {
result.push_back(cur->val);
stk.pop();
}
pre = cur;
}
return result;
}
};
// iterative solution: no optimal (It breaks the basic structure of the tree)
class Solution {
public:
vector<int> postorderTraversal(TreeNode *root) {
vector<TreeNode *> stk;
vector<int> ret;
if (root == nullptr) {
return ret;
}
stk.push_back(root);
while (!stk.empty()) {
TreeNode cur(stk.back()->val);
cur.left = stk.back()->left;
cur.right = stk.back()->right;
// break the pointers to it children to meet the next predicate
stk.back()->left = stk.back()->right = nullptr;
if (cur.left == nullptr && cur.right == nullptr) {
ret.push_back(cur.val);
stk.pop_back();
}
if (cur.right != nullptr) {
stk.push_back(cur.right);
}
if (cur.left != nullptr) {
stk.push_back(cur.left);
}
}
return ret;
}
};