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

简介: 二叉树的递归遍历(力扣刷题)
  1. 确定递归函数的参数和返回值: 确定哪些参数是递归的过程中需要处理的,那么就在递归函数里加上这个参数, 并且还要明确每次递归的返回值是什么进而确定递归函数的返回类型。


  1. 确定终止条件: 写完了递归算法, 运行的时候,经常会遇到栈溢出的错误,就是没写终止条件或者终止条件写的不对,操作系统也是用一个栈的结构来保存每一层递归的信息,如果递归没有终止,操作系统的内存栈必然就会溢出。


  1. 确定单层递归的逻辑: 确定每一层递归需要处理的信息。在这里也就会重复调用自己来实现递归的过程。


前序遍历:

/**
 * 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;
    }
};


相关文章
|
2天前
leetcode代码记录(二叉树的所有路径
leetcode代码记录(二叉树的所有路径
8 0
|
2天前
|
算法 C++
【刷题】Leetcode 1609.奇偶树
这道题是我目前做过最难的题,虽然没有一遍做出来,但是参考大佬的代码,慢慢啃的感觉的真的很好。刷题继续!!!!!!
6 0
|
2天前
|
算法 索引
【刷题】滑动窗口精通 — Leetcode 30. 串联所有单词的子串 | Leetcode 76. 最小覆盖子串
经过这两道题目的书写,相信大家一定深刻认识到了滑动窗口的使用方法!!! 下面请大家继续刷题吧!!!
8 0
|
3天前
|
算法
【刷题】 leetcode 面试题 08.05.递归乘法
递归算法是一种在计算机科学和数学中广泛应用的解决问题的方法,其基本思想是利用问题的自我相似性,即将一个大问题分解为一个或多个相同或相似的小问题来解决。递归算法的核心在于函数(或过程)能够直接或间接地调用自身来求解问题的不同部分,直到达到基本情况(也称为基础案例或终止条件),这时可以直接得出答案而不必再进行递归调用。
20 4
【刷题】 leetcode 面试题 08.05.递归乘法
|
3天前
|
存储 算法 安全
【刷题】 leetcode 面试题 01.06 字符串压缩
来看效果: 非常好!!!过啦!!!
25 5
【刷题】 leetcode 面试题 01.06 字符串压缩
|
3天前
|
存储 算法 测试技术
|
3天前
|
算法 C语言 C++
|
3天前
leetcode代码记录(对称二叉树 中序遍历+回文串 为什么不行
leetcode代码记录(对称二叉树 中序遍历+回文串 为什么不行
7 0
|
3天前
leetcode代码记录(二叉树的最小深度
leetcode代码记录(二叉树的最小深度
7 0

热门文章

最新文章