-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
98 lines (92 loc) · 1.95 KB
/
main.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
#include <iostream>
#include <vector>
#include <algorithm>
#include <stack>
using namespace std;
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
// morris
class Solution {
public:
vector<int> inorderTraversal(TreeNode* root) {
vector<int> res;
while(root)
{
if(root->left)
{
TreeNode* pre = root -> left;
while(pre->right && pre->right != root)
{
pre = pre -> right;
}
if(!pre->right)
{
pre->right = root;
root = root -> left;
}
else
{
pre->right = NULL;
res.push_back(root->val);
root = root -> right;
}
}
else
{
res.push_back(root->val);
root = root -> right;
}
}
return res;
}
};
// ·ÇµÝ¹éд·¨
/*
class Solution {
public:
vector<int> inorderTraversal(TreeNode* root) {
vector<int> res;
stack<TreeNode*> st;
while(root || !st.empty())
{
while(root)
{
st.push(root);
root = root -> left;
}
root = st.top();
st.pop();
res.push_back(root->val);
root = root -> right;
}
return res;
}
};
*/
// µÝ¹éд·¨
/*
class Solution {
public:
vector<int> inorderTraversal(TreeNode* root) {
vector<int> res;
inorder(root, res);
return res;
}
void inorder(TreeNode* root, vector<int>& res)
{
if(root == NULL)
return ;
inorder(root->left, res);
res.push_back(root->val);
inorder(root->right, res);
}
};
*/
int main()
{
return 0;
}