-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path145.postorderTraversal.cpp
More file actions
executable file
·66 lines (61 loc) · 1.65 KB
/
145.postorderTraversal.cpp
File metadata and controls
executable file
·66 lines (61 loc) · 1.65 KB
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
/*
@filename 145.postorderTraversal.cpp
@author caonan
@date 2022-04-14 07:39:59
@reference leetcode 145
@url https://leetcode-cn.com/problems/binary-tree-postorder-traversal/
@brief 给你一棵二叉树的根节点 root ,返回其节点值的 后序遍历 。
*/
#include <assert.h>
#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <map>
#include <stack>
#include <vector>
#include "traverse.h"
using namespace std;
// Definition for a binary tree node.
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode() : val(0), left(nullptr), right(nullptr) {}
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};
class Solution
{
public:
//非递归版 时间o(n) 空间o(n)
vector<int> postorderTraversal(TreeNode *root)
{
auto cur = root;
TreeNode *pre = nullptr;
stack<TreeNode *> tree_stack;
vector<int> ans;
while (cur || !tree_stack.empty()) {
while (cur) {
tree_stack.push(cur);
cur = cur->left;
}
cur = tree_stack.top();
if (cur->right != pre && cur->right) {
cur = cur->right;
} else {
ans.push_back(cur->val);
pre = cur;
cur = nullptr;
tree_stack.pop();
}
}
return ans;
}
};
// todo:还有一种空间只要o(1)的
class Solution1 : Solution
{
public:
vector<int> postorderTraversal(TreeNode *root) {}
};
int main() { return 0; }