1.前序遍历
前序遍历:根->左->右 (搜索和创建一颗新的树)
1.递归法:调整访问顺序
class Solution1{
public:
vector<int> preTraversal(TreeNode* root){
vector<int> ret;
preHelper(ret,root);
return ret;
}
private:
void preHelper(vector<int>& ret,TreeNode* root){
if(root==NULL) return;
ret.push_back(root->val);
preHelper(ret,root->left);
preHelper(ret,root->right);
}
};
2.迭代法:
将访问的节点放入栈中,把要处理的节点也放入栈中但是要做标记,标记就是要处理的节点放入栈之后,紧接着放入一个空指针作为标记。
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<int> preorderTraversal(TreeNode* root) {
vector<int> result;
stack<TreeNode*> st;
if(root!=NULL) st.push(root);
while(!st.empty()){
TreeNode* node=st.top();
if(node!=NULL){
st.pop();
if(node->right) st.push(node->right);//右
if(node->left) st.push(node->left);//左
st.push(node);//中
st.push(NULL);//标记
}
else{
st.pop();
node=st.top();
st.pop();
result.push_back(node->val);
}
}
return result;
}
};
2.中序遍历
中序遍历 左->根->右 (中序遍历二叉搜素树 是从小到大的顺序链接:Leecode例题 )
1.递归法:递归的终止条件是当前节点是否为空。首先递归调用遍历左子树,然后访问当前节点,最后递归调用右子树。
class Solution1{
public:
vector<int> inorderTraversal(TreeNode* root)
vector<int> ret;
inorderHelper(ret,root);
return ret;
private:
void inorderHelper(vector<int>& ret,TreeNode* root){
if(root==NULL) return;
inorderHelper(ret,root->left);
ret.push_back(root->value);
inorderHelper(ret,root->right);
}
};
2.迭代法:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<int> inorderTraversal(TreeNode* root) {
stack<TreeNode*> st;
vector<int> result;
if(root!=NULL)
st.push(root);
while(!st.empty()){
TreeNode* node=st.top();
if(node!=NULL){
st.pop();
if(node->right) st.push(node->right);//右
st.push(node);//中
st.push(NULL);
if(node->left) st.push(node->left);//左
}
else{
st.pop();
node=st.top();
st.pop();
result.push_back(node->val);
}
}
return result;
}
};
3.后序遍历
后序遍历:左->右->根
1.递归法
class Solution1{
public:
vector<int> postorderTraversal(TreeNode* root)
vector<int> ret;
postorderHelper(ret,root);
return ret;
private:
void postorderHelper(vector<int>& ret,TreeNode* root){
if(root==NULL)
return;
postorderHelper(ret,root->left);
postorderHelper(ret,root->right);
ret.push_back(root->val);
}
}
2.迭代法:
①
/**
* 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;
if(root!=NULL) st.push(root);
while(!st.empty()){
TreeNode* node=st.top();
if(node!=NULL){
st.pop();
st.push(node);//中
st.push(NULL);
if(node->right) st.push(node->right);//右
if(node->left) st.push(node->left);//左
}
else{
st.pop();
node=st.top();
st.pop();
result.push_back(node->val);
}
}
return result;
}
};
②按照根、右、左的顺序访问,然后将结果取反即可。后序遍历的顺序是左、右、根。这种方法就可以在前序遍历的基础上修改即可。
class Solution3 {
public:
vector<int> postorderTraversal(TreeNode* root) {
vector<int> ret;
if(root==NULL)return ret;
stack<TreeNode*> st;
st.push(root);
while(!st.empty())
{
TreeNode *curr=st.top();
st.pop();
if(curr->left)st.push(curr->left);
if(curr->right)st.push(curr->right);
ret.push_back(curr->val);
}
reverse(ret.begin(),ret.end());
return ret;
}
};