forked from haoel/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
binaryTreePostorderTraversal.cpp
152 lines (135 loc) · 3.28 KB
/
binaryTreePostorderTraversal.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// Source : https://oj.leetcode.com/problems/binary-tree-postorder-traversal/
// Author : Hao Chen
// Date : 2014-07-21
/**********************************************************************************
*
* 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?
*
**********************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <iostream>
#include <vector>
using namespace std;
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
vector<int> postorderTraversal1(TreeNode *root);
vector<int> postorderTraversal2(TreeNode *root);
vector<int> postorderTraversal(TreeNode *root) {
if (random()%2){
cout << "---method one---" << endl;
return postorderTraversal1(root);
}
cout << "---method two---" << endl;
return postorderTraversal2(root);
}
vector<int> postorderTraversal1(TreeNode *root) {
vector<int> v;
vector<TreeNode*> stack;
if (root) {
stack.push_back(root);
}
while (stack.size()>0){
TreeNode *n = stack.back();
stack.pop_back();
v.insert(v.begin(), n->val);
if (n->left){
stack.push_back(n->left);
}
if (n->right) {
stack.push_back(n->right);
}
}
return v;
}
vector<int> postorderTraversal2(TreeNode *root) {
vector<int> v;
vector<TreeNode*> stack;
TreeNode *node = root;
TreeNode *lastVisitNode = NULL;
while(stack.size()>0 || node!=NULL){
if (node != NULL){
stack.push_back(node);
node = node->left;
}else{
TreeNode *n = stack.back();
if (n->right != NULL && lastVisitNode != n->right){
node = n->right;
}else{
stack.pop_back();
v.push_back(n->val);
lastVisitNode = n;
}
}
}
return v;
}
TreeNode* createTree(int a[], int n)
{
if (n<=0) return NULL;
TreeNode **tree = new TreeNode*[n];
for(int i=0; i<n; i++) {
if (a[i]==0 ){
tree[i] = NULL;
continue;
}
tree[i] = new TreeNode(a[i]);
}
int pos=1;
for(int i=0; i<n && pos<n; i++) {
if (tree[i]){
tree[i]->left = tree[pos++];
if (pos<n){
tree[i]->right = tree[pos++];
}
}
}
return tree[0];
}
void printTree_post_order(TreeNode *root)
{
if (root == NULL){
//cout << "# ";
return ;
}
printTree_post_order(root->left);
printTree_post_order(root->right);
cout << root->val << " ";
}
void printArray(vector<int> v)
{
for(int i=0; i<v.size(); i++){
cout << v[i] << " ";
}
cout << endl;
}
int main()
{
srand(time(0));
int a[] = {1,2,3,4,5,0,6,0,0,7,8,9,0};
TreeNode* p = createTree(a, sizeof(a)/sizeof(int));
printTree_post_order(p);
cout << endl;
vector<int> v = postorderTraversal(p);
printArray(v);
cout << endl;
return 0;
}