leetcode94二叉树的中序遍历

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

递归

时间复杂度和空间复杂度均为 O(n),其中 n 是二叉树中节点的数量。具体分析如下:

遍历整棵二叉树,对于每个节点,都要执行一次递归操作(递归到左子树、插入当前节点、递归到右子树),因此时间复杂度为 O(n)。

由于需要使用一个数组来存储遍历结果,数组的长度也为 n,因此空间复杂度为 O(n)。

因此,总的时间复杂度和空间复杂度均为 O(n)。

执行用时:0 ms, 在所有 C++ 提交中击败了100.00%的用户

内存消耗:8 MB, 在所有 C++ 提交中击败了94.05%的用户

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

迭代

时间复杂度为O(n),其中n为树中节点的个数,因为每个节点都会被遍历一次。

空间复杂度也为O(n),因为最坏情况下需要将所有的节点都压入栈中,栈的大小为n。

执行用时:0 ms, 在所有 C++ 提交中击败了100.00%的用户

内存消耗:8.1 MB, 在所有 C++ 提交中击败了69.26%的用户

/**
 * 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;
        TreeNode* mos = nullptr;
        while (root != nullptr) {
            if (root->left != nullptr) {
                mos = root->left;
                while (mos->right != nullptr && mos->right != root) {
                    mos = mos->right;
                }
                if (mos->right == nullptr) {
                    mos->right = root;
                    root = root->left;
                }
                else {
                    mos->right = nullptr;
                    res.push_back(root->val);
                    root = root->right;
                }
            }
            else {
                res.push_back(root->val);
                root = root->right;
            }
        }
        return res;
    }
};

Morris遍历

时间复杂度为O(n),其中n为树中节点的个数,因为每个节点都会被遍历一次。空间复杂度为O(1),因为没有使用额外的数据结构,只使用了常数级别的辅助空间。

执行用时:0 ms, 在所有 C++ 提交中击败了100.00%的用户

内存消耗:8.1 MB, 在所有 C++ 提交中击败了78.54%的用户

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值