二叉树的递归遍历(力扣刷题)

  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>& vec)
//     {
//         if(cur == NULL) return;
//         vec.push_back(cur->val);
//         traversal(cur->left,vec);
//         traversal(cur->right,vec);
//     }

//     vector<int> preorderTraversal(TreeNode* root) {
//         vector<int> result;
//         traversal(root,result);
//         return result;
//     }
// };


//非递归法
class Solution
{
public:
    vector<int> preorderTraversal(TreeNode* root)
    {
        stack<TreeNode*> st;
        vector<int> result;

        if(root == NULL)
        {
            return result;
        }

        st.push(root);

        while(!st.empty())
        {
            TreeNode* node = st.top();
            st.pop();
            result.push_back(node->val);

            if(node->right)
            {
                st.push(node->right);
            }

            if(node->left)
            {
                st.push(node->left);
            }  
        }
        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:
//     void traversal(TreeNode* cur, vector<int>& vec) 
//     {
//          if (cur == NULL) return;

//          traversal(cur->left, vec);  // 左
//          vec.push_back(cur->val);    // 中
//          traversal(cur->right, vec); // 右
//     }
//     vector<int> inorderTraversal(TreeNode* root) 
//     {
//         vector<int> result;
//         traversal(root, result);
//         return result;
//     }
// };

//非递归法
class Solution
{
public:
    vector<int> inorderTraversal(TreeNode* root)
    {
        vector<int> result;
        stack<TreeNode*> st;
        TreeNode* cur = root;

        while(cur != NULL || !st.empty())
        {
            //一直左遍历
            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;
    }
};



后序遍历:

/**
 * 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>& vec) 
//     {
//         if (cur == NULL) return;
//         traversal(cur->left, vec);  // 左
//         traversal(cur->right, vec); // 右
//         vec.push_back(cur->val);    // 中
//     }

//     vector<int> postorderTraversal(TreeNode* root) {
//         vector<int> result;
//         traversal(root,result);
//         return result;
//     }
// };


//非递归法
class Solution
{
public:
    vector<int> postorderTraversal(TreeNode* root)
    {
        stack<TreeNode*> st;
        vector<int> result;

        if(root == NULL)
        {
            return result;
        }

        st.push(root);

        while(!st.empty())
        {
            TreeNode* node = st.top();
            st.pop();
            result.push_back(node->val);

            if(node->left)
            {
                st.push(node->left);
            }

            if(node->right)
            {
                st.push(node->right);
            } 
        }

        reverse(result.begin(),result.end());

        return result;
    }
};


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

会飞的鱼-blog

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值