根据二叉树的根节点root,返回它的前序遍历
前序遍历:中左右
递归三部曲
1) 确定递归函数的参数和返回值
2)确定终止条件
3)确定单层递归逻辑
伪代码
代码
/**
* 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:
void traversal(TreeNode* cur,vector<int>& result){
//终止条件
if(cur==NULL){
return;
}
//单层递归逻辑
//前序:中左右
result.push_back(cur->val);//中
traversal(cur->left,result);//左
traversal(cur->right,result);//右
}
vector<int> preorderTraversal(TreeNode* root) {
vector<int> result;
traversal(root,result);
return result;
}
};
代码
/**
* 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:
vector<int> preorderTraversal(TreeNode* root) {
vector<int> result;
stack<TreeNode*> st;
st.push(root);
while(!st.empty()){
TreeNode* node = st.top();
st.pop();
if(node!=NULL){
result.push_back(node->val);
}
else{
continue;
}
if(node->right){
st.push(node->right);
}
if(node->left){
st.push(node->left);
}
}
return result;
}
};
根据二叉树的根节点root,返回它的后序遍历
后序遍历:左右中
递归三部曲:
1) 确定递归函数的参数和返回值
2)确定终止条件
3)确定单层递归逻辑
伪代码
代码
/**
* 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:
void traversal(TreeNode* cur,vector<int>& result){
//终止条件
if(cur==NULL){
return;
}
//单层递归逻辑
traversal(cur->left,result);//左
traversal(cur->right,result);//右
result.push_back(cur->val);//中
}
vector<int> postorderTraversal(TreeNode* root){
vector<int> result;
traversal(root,result);
return result;
}
};
代码
/**
* 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:
vector<int> postorderTraversal(TreeNode* root){
vector<int> result;
stack<TreeNode*> st;
st.push(root);
//终止条件
while(!st.empty()){
TreeNode* node = st.top();
st.pop();
if(node!=NULL){
result.push_back(node->val);
}
else{
continue;
}
if(node->left){
st.push(node->left);
}
if(node->right){
st.push(node->right);
}
}
reverse(result.begin(),result.end());
return result;
}
};
根据二叉树的根节点root,返回它的后序遍历
中序遍历:左中右
递归三部曲:
1) 确定递归函数的参数和返回值
2)确定终止条件
3)确定单层递归逻辑
伪代码
代码
/**
* 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:
void traversal(TreeNode* cur,vector<int>& result){
//终止条件
if(cur==NULL){
return;
}
//中序遍历,左中右
traversal(cur->left,result);//左
result.push_back(cur->val);//中
traversal(cur->right,result);//右
}
vector<int> inorderTraversal(TreeNode* root) {
vector<int> result;
traversal(root,result);
return result;
}
};
如果按照前序遍历和后序遍历的迭代法来,那么访问的元素和要处理的元素顺序不一致
所以,需要一个指针cur指针遍历节点,栈处理遍历过的节点
代码
/**
* 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:
vector<int> inorderTraversal(TreeNode* root) {
vector<int> result;
stack<TreeNode*> st;
TreeNode* cur = root;
while(!st.empty() || cur!=NULL){
//向左一直遍历
if(cur!=NULL){
st.push(cur);
cur = cur->left;//左
}
//向左遍历遇到空节点
else{
//加入当前节点,相当于中节点,并更新当前指针
cur = st.top();
st.pop();
result.push_back(cur->val);//中
//遍历当前节点的右孩子
cur = cur->right;//也是一颗二叉树,继续同样的操作(一直向左,当前节点,右孩子)
}
}
return result;
}
};