144. 二叉树的前序遍历
Problem: 144. 二叉树的前序遍历
前序:中左右!
Code
递归方法
/**
* 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 {
vector<int> res;
public:
vector<int> preorderTraversal(TreeNode* root) {
preTraver(root);
return res;
}
void preTraver(TreeNode* root){
if(root==nullptr){//递归边界
return;
}
res.push_back(root->val);//前序遍历(先操作当前节点)
preTraver(root->left);
preTraver(root->right);
}
};
迭代法
/**
* 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> res;
stack<TreeNode*> st;
if(root==nullptr) return res;
st.push(root);
while(!st.empty()){
//入栈:跟节点->右孩子->左孩子
//每次访问,先出现一个节点(在压入这个节点的右孩子和左孩子),这样在下次访问下一个节点的时候,先访问的会是左孩子
TreeNode* cur = st.top();
st.pop();
int num = cur->val;
res.push_back(num);
if(cur->right!=nullptr) st.push(cur->right);
if(cur->left!=nullptr) st.push(cur->left);
}
return res;
}
};
145. 二叉树的后序遍历
Problem: 145. 二叉树的后序遍历
左右中
Code
递归方法
/**
* 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 {
vector<int> res;
public:
vector<int> postorderTraversal(TreeNode* root) {
postTravel(root);
return res;
}
//左右中
void postTravel(TreeNode* cur){
if(cur == nullptr) return;
postTravel(cur->left);
postTravel(cur->right);
res.push_back(cur->val);
}
};
迭代法
/**
* 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> res;
if(root==nullptr) return res;
stack<TreeNode*> st;
st.push(root);
while(!st.empty()){
TreeNode* cur = st.top();
st.pop();
res.push_back(cur->val);
if(cur->left!=nullptr) st.push(cur->left);
if(cur->right!=nullptr) st.push(cur->right);
}
reverse(res.begin(), res.end());
return res;
}
};
二叉树的中序遍历
Problem: 94. 二叉树的中序遍历
左中右
Code
递归法
/**
* 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 {
vector<int> res;
public:
vector<int> inorderTraversal(TreeNode* root) {
inorder(root);
return res;
}
void inorder(TreeNode* cur){
if(cur == nullptr) return;
inorder(cur->left);
res.push_back(cur->val);
inorder(cur->right);
}
};
迭代法:中序遍历的迭代法不是很好理解,但是思想还是左右中
栈做辅助
/**
* 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> res;
if(root==nullptr) return res;
stack<TreeNode*> st;
//左中右(左子树),只要有左孩子,就一直压入栈
TreeNode* cur = root;
while(!st.empty() || cur != nullptr){
//这里依旧是左中右的访问顺序,左的操作是,只要左子树非空,就左孩子
//左子树为空,就需要访问当前的元素(中),在压入右子树
if(cur!=nullptr){
st.push(cur);
cur = cur->left;
}else{
cur = st.top();
st.pop();
res.push_back(cur->val);
cur = cur->right;
}
}
return res;
}
};