递归
class Solution {
public:
vector<int> ans;
void traversal(TreeNode* cur) {
if (nullptr == cur) {
return;
}
ans.push_back(cur->val);
traversal(cur->left);
traversal(cur->right);
}
vector<int> preorderTraversal(TreeNode *root) {
ans.clear();
if (nullptr == root) return ans;
traversal(root);
return ans;
}
};
迭代
class Solution {
public:
vector<int> preorderTraversal(TreeNode *root) {
stack<TreeNode *> st;
vector<int> ans;
if (nullptr != root) st.push(root);
while (!st.empty()) {
TreeNode *cur = st.top();
st.pop();
ans.push_back(cur->val);
if (cur->right) st.push(cur->right);
if (cur->left) st.push(cur->left);
}
return ans;
}
};
统一迭代
class Solution {
public:
vector<int> preorderTraversal(TreeNode *root) {
stack<TreeNode*> st;
vector<int> ans;
if (nullptr != root) st.push(root);
while (!st.empty()) {
TreeNode *cur = st.top();
st.pop();
if (nullptr != cur) {
if (cur->right) st.push(cur->right);
if (cur->left) st.push(cur->left);
st.push(cur);
st.push(nullptr);
} else {
cur = st.top();
st.pop();
ans.push_back(cur->val);
}
}
return ans;
}
};
递归
class Solution {
public:
vector<int> ans;
void traversal(TreeNode *cur) {
if (nullptr == cur) return;
traversal(cur->left);
ans.push_back(cur->val);
traversal(cur->right);
}
vector<int> inorderTraversal(TreeNode *root) {
ans.clear();
if (nullptr == root) return ans;
traversal(root);
return ans;
}
};
迭代
class Solution {
public:
vector<int> inorderTraversal(TreeNode *root) {
vector<int> ans;
stack<TreeNode*> st;
TreeNode *cur = root;
while (cur != nullptr || !st.empty()) {
if (nullptr != cur) {
st.push(cur);
cur = cur->left;
} else {
cur = st.top(); st.pop();
ans.push_back(cur->val);
cur = cur->right;
}
}
return ans;
}
};
统一迭代
class Solution {
public:
vector<int> inorderTraversal(TreeNode *root) {
stack<TreeNode*> st;
vector<int> ans;
if (nullptr != root) st.push(root);
while (!st.empty()) {
TreeNode *cur = st.top();
st.pop();
if (nullptr != cur) {
if (cur->right) st.push(cur->right);
st.push(cur);
st.push(nullptr);
if (cur->left) st.push(cur->left);
} else {
cur = st.top();
st.pop();
ans.push_back(cur->val);
}
}
return ans;
}
};
递归
class Solution {
public:
vector<int> ans;
void traversal(TreeNode *cur) {
if (nullptr == cur) return;
traversal(cur->left);
traversal(cur->right);
ans.push_back(cur->val);
}
vector<int> postorderTraversal(TreeNode *root) {
ans.clear();
if (nullptr == root) return ans;
traversal(root);
return ans;
}
};
迭代
class Solution {
public:
vector<int> postorderTraversal(TreeNode* root) {
stack<TreeNode*> st;
vector<int> ans;
if (nullptr != root) st.push(root);
while (!st.empty()) {
TreeNode* cur = st.top();
st.pop();
ans.push_back(cur->val);
if (cur->left) st.push(cur->left);
if (cur->right)st.push(cur->right);
}
reverse(ans.begin(), ans.end());
return ans;
}
};
统一迭代
class Solution {
public:
vector<int> postorderTraversal(TreeNode *root) {
stack<TreeNode*> st;
vector<int> ans;
if (nullptr != root) st.push(root);
while (!st.empty()) {
TreeNode *cur = st.top();
st.pop();
if (nullptr != cur) {
st.push(cur);
st.push(nullptr);
if (cur->right) st.push(cur->right);
if (cur->left) st.push(cur->left);
} else {
cur = st.top();
st.pop();
ans.push_back(cur->val);
}
}
return ans;
}
};