方法1:递归法
class Solution {
private:
void traversal(TreeNode* cur, vector<int>& vec) {
if(cur == NULL) return;
vec.emplace_back(cur->val);
if(cur->left) traversal(cur->left, vec);
if(cur->right) traversal(cur->right, vec);
return;
}
public:
vector<int> preorderTraversal(TreeNode* root) {
vector<int>res;
if(root == NULL) return res;
traversal(root, res);
return res;
}
};
$时间复杂度O(n),空间复杂度O(n)
方法2:迭代法
class Solution {
public:
vector<int> preorderTraversal(TreeNode* root) {
stack<TreeNode*> st;
vector<int> res;
if (root != NULL) st.push(root);
while(!st.empty()) {
TreeNode* nod = st.top();
if (nod != NULL) {
st.pop();
if (nod->right) st.push(nod->right);
if (nod->left) st.push(nod->left);
st.push(nod);
st.push(NULL);
} else {
st.pop();
nod = st.top();
st.pop();
res.emplace_back(nod->val);
}
}
return res;
}
};
$时间复杂度O(n),空间复杂度O(n)
方法3:Morris 遍历
class Solution {
public:
vector<int> preorderTraversal(TreeNode* root) {
vector<int> res;
TreeNode* cur = nullptr;
while (root != nullptr) {
if (root->left != nullptr) {
cur = root->left;
while (cur->right != nullptr && cur->right != root) cur = cur->right;
if (cur->right == nullptr) {
res.emplace_back(root->val);
cur->right = root;
root = root->left;
continue;
} else {
cur->right = nullptr;
}
} else {
res.emplace_back(root->val);
}
root = root->right;
}
return res;
}
};
$时间复杂度O(n),空间复杂度O(1)
方法1:递归法
class Solution {
private:
void traversal(TreeNode* cur, vector<int>& vec) {
if(cur == NULL) return;
if(cur->left) traversal(cur->left, vec);
vec.emplace_back(cur->val);
if(cur->right) traversal(cur->right, vec);
return;
}
public:
vector<int> inorderTraversal(TreeNode* root) {
vector<int> res;
if (root == NULL) return res;
traversal(root, res);
return res;
}
};
$时间复杂度O(n),空间复杂度O(n)
方法2:迭代法
class Solution {
public:
vector<int> inorderTraversal(TreeNode* root) {
stack<TreeNode*> st;
vector<int> res;
if (root != NULL) st.push(root);
while(!st.empty()) {
TreeNode* nod = st.top();
if (nod != NULL) {
st.pop();
if (nod->right) st.push(nod->right);
st.push(nod);
st.push(NULL);
if (nod->left) st.push(nod->left);
} else {
st.pop();
nod = st.top();
st.pop();
res.emplace_back(nod->val);
}
}
return res;
}
};
$时间复杂度O(n),空间复杂度O(n)
方法3:Morris 中序遍历
class Solution {
public:
vector<int> inorderTraversal(TreeNode* root) {
vector<int> res;
TreeNode* cur = nullptr;
if (root == nullptr) return res;
while (root != nullptr) {
if (root->left != nullptr) {
cur = root->left;
while (cur->right != nullptr && cur->right != root) {
cur = cur->right;
}
if (cur->right == nullptr) {
cur->right = root;
root = root->left;
} else {
res.emplace_back(root->val);
root = root->right;
cur->right = nullptr;
}
} else {
res.emplace_back(root->val);
root = root->right;
}
}
return res;
}
};
$时间复杂度O(n),空间复杂度O(1)
方法1:递归法
class Solution {
private:
void traversal(TreeNode* cur, vector<int>& vec) {
if (cur == NULL) return;
if (cur->left) traversal(cur->left, vec);
if (cur->right) traversal(cur->right, vec);
vec.emplace_back(cur->val);
return;
}
public:
vector<int> postorderTraversal(TreeNode* root) {
vector<int> res;
if (root == NULL) return res;
traversal(root, res);
return res;
}
};
$时间复杂度O(n),空间复杂度O(n)
方法2:迭代法
class Solution {
public:
vector<int> postorderTraversal(TreeNode* root) {
stack<TreeNode*> st;
vector<int> res;
if (root != NULL) st.push(root);
while(!st.empty()) {
TreeNode* nod = st.top();
if (nod != NULL) {
st.pop();
st.push(nod);
st.push(NULL);
if (nod->right) st.push(nod->right);
if (nod->left) st.push(nod->left);
} else {
st.pop();
nod = st.top();
st.pop();
res.emplace_back(nod->val);
}
}
return res;
}
};
$时间复杂度O(n),空间复杂度O(n)
方法3:Morris
class Solution {
private:
void reverseTree(vector<int>& vec, TreeNode* nod) {
int cnt = 0;
while (nod != nullptr) {
vec.emplace_back(nod->val);
nod = nod->right;
++cnt;
}
reverse(vec.end() - cnt, vec.end());
}
public:
vector<int> postorderTraversal(TreeNode* root) {
vector<int> res;
TreeNode* cur = nullptr, * ver = root;
if (ver == nullptr) return res;
while (ver != nullptr) {
if (ver->left != nullptr) {
cur = ver->left;
while (cur->right != nullptr && cur->right != ver) cur = cur->right;
if (cur->right == nullptr) {
cur->right = ver;
ver = ver->left;
continue;
} else {
cur->right = nullptr;
reverseTree(res, ver->left);
}
}
ver = ver->right;
}
reverseTree(res, root);
return res;
}
};
$时间复杂度O(n),空间复杂度O(1)