-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary-tree-postorder.cpp
More file actions
39 lines (33 loc) · 942 Bytes
/
binary-tree-postorder.cpp
File metadata and controls
39 lines (33 loc) · 942 Bytes
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
// 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?
//// 巧妙的方法:前序遍历 根->左->右 变成 根->右->左 结果再reverse一下
class Solution {
public:
vector<int> postorderTraversal(TreeNode *root) {
vector<int> res;
if(!root)
return res;
stack<TreeNode*> st;
st.push(root);
while (st.size())
{
TreeNode* temp = st.top();
st.pop();
res.push_back(temp->val);
if(temp->left)
st.push(temp->left);
if(temp->right)
st.push(temp->right);
}
reverse(res.begin(), res.end());
return res;
}
};