力扣--二叉树的遍历题


这几题都是关于二叉树的遍历,前序中序后序,虽然比较基础,但还是做个笔记吧。代码都是官解的代码。

【前序遍历】

144. 二叉树的前序遍历
前序遍历官解

【递归法代码】

/**
 * 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 preorder(TreeNode *root, vector<int> &res) {
        if (root == nullptr) {
            return;
        }
        res.push_back(root->val);
        preorder(root->left, res);
        preorder(root->right, res);
    }

    vector<int> preorderTraversal(TreeNode *root) {
        vector<int> res;
        preorder(root, res);
        return res;
    }
};

【迭代法代码】

class Solution {
public:
    vector<int> preorderTraversal(TreeNode* root) {
        vector<int> res;
        if (root == nullptr) {
            return res;
        }

        stack<TreeNode*> stk;
        TreeNode* node = root;
        while (!stk.empty() || node != nullptr) {
            while (node != nullptr) {
                res.emplace_back(node->val);
                stk.emplace(node);
                node = node->left;
            }
            node = stk.top();
            stk.pop();
            node = node->right;
        }
        return res;
    }
};

【后序遍历】

145. 二叉树的后序遍历
后序遍历官解

【递归法代码】

class Solution {
public:
    void postorder(TreeNode *root, vector<int> &res) {
        if (root == nullptr) {
            return;
        }
        postorder(root->left, res);
        postorder(root->right, res);
        res.push_back(root->val);
    }

    vector<int> postorderTraversal(TreeNode *root) {
        vector<int> res;
        postorder(root, res);
        return res;
    }
};

【迭代法代码】

class Solution {
public:
    vector<int> postorderTraversal(TreeNode *root) {
        vector<int> res;
        if (root == nullptr) {
            return res;
        }

        stack<TreeNode *> stk;
        TreeNode *prev = nullptr;
        while (root != nullptr || !stk.empty()) {
            while (root != nullptr) {
                stk.emplace(root);
                root = root->left;
            }
            root = stk.top();
            stk.pop();
            if (root->right == nullptr || root->right == prev) {
                res.emplace_back(root->val);
                prev = root;
                root = nullptr;
            } else {
                stk.emplace(root);
                root = root->right;
            }
        }
        return res;
    }
};

【中序遍历】

【递归法】

class Solution {
public:

    void inorder(TreeNode*root, vector<int>& res){
        if(root==nullptr){
            return ;
        }
        inorder(root->left,res);
        res.push_back(root->val);
        inorder(root->right,res);
    }
    vector<int> inorderTraversal(TreeNode* root) {
        vector<int> res;
        inorder(root,res);
        return res;
    }
};

【迭代法】

class Solution {
public:
    vector<int> inorderTraversal(TreeNode* root) {
        vector<int> res;
        stack<TreeNode*> stk;
        while (root != nullptr || !stk.empty()) {
            while (root != nullptr) {
                stk.push(root);
                root = root->left;
            }
            root = stk.top();
            stk.pop();
            res.push_back(root->val);
            root = root->right;
        }
        return res;
    }
};
### LeetCode C++ 二叉树中序遍历实现 以下是基于给定引用内容以及专业知识所提供的解决方案。 #### 方法一:递归方法 递归是最直观的方式之一来完成二叉树的中序遍历。按照定义,先访问左子树,接着访问根节点,最后访问右子树[^1]。 ```cpp #include <vector> using namespace std; // 定义二叉树节点结构 class TreeNode { public: 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) {} }; void inorderTraversalHelper(TreeNode* root, vector<int>& result) { if (root == nullptr) return; // 如果当前节点为空,则返回 inorderTraversalHelper(root->left, result); // 访问左子树 result.push_back(root->val); // 访问根节点 inorderTraversalHelper(root->right, result); // 访问右子树 } vector<int> inorderTraversal(TreeNode* root) { vector<int> result; inorderTraversalHelper(root, result); return result; } ``` 上述代码通过辅助函数 `inorderTraversalHelper` 来递归地处理每一个节点及其子树[^2]。 --- #### 方法二:迭代方法(栈) 如果希望避免显式的递归调用,可以使用栈模拟递归过程。这种方法同样能够有效地实现中序遍历[^5]。 ```cpp #include <stack> vector<int> inorderTraversalIterative(TreeNode* root) { vector<int> result; stack<TreeNode*> s; TreeNode* current = root; while (current != nullptr || !s.empty()) { // 不断向左走并将沿途节点压入栈中 while (current != nullptr) { s.push(current); current = current->left; } // 当前无左子树可访问时,弹出栈顶并访问它 current = s.top(); s.pop(); result.push_back(current->val); // 转而访问右子树 current = current->right; } return result; } ``` 此方法利用栈保存尚未完全访问过的节点,在每次循环中优先探索左侧路径直到尽头后再回溯访问父节点和右侧分支[^4]。 --- #### 复杂度分析 - **时间复杂度**: O(n),其中 n 是二叉树中的节点总数。无论是递归还是迭代方式都需要访问每个节点一次。 - **空间复杂度**: - 对于递归版本来说,其额外开销取决于递归深度,最坏情况下为 O(h),h 表示树的高度; - 迭代版则需借助显式的数据结构——栈存储部分未处理完毕的节点信息,因此平均情况下的空间消耗也为 O(h)[^3]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

一颗小芋圆

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

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

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

打赏作者

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

抵扣说明:

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

余额充值