LeetCode 热题 HOT 100 第九天

这篇博客介绍了如何利用前序与中序遍历序列构造二叉树,以及如何将二叉树展开为链表。此外,还探讨了在股票交易中寻找最佳买入卖出时机的动态规划策略,以及在二叉树中找到最大路径和的深度优先搜索方法。通过这些实例,深入理解数据结构和算法在实际问题中的应用。

Leetcode105. 从前序与中序遍历序列构造二叉树

/**
 * 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:
    unordered_map<int, int> hash;
    TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
        int n = preorder.size();
        for (int i = 0; i < n; i ++ ) hash[inorder[i]] = i;
        return build(preorder, 0, n - 1, 0, n - 1);
    }

    TreeNode* build(vector<int>& preorder, int pl, int pr, int il, int ir) {
        if (pl > pr) return nullptr;
        auto root = new TreeNode(preorder[pl]);
        int pos = hash[root->val];

        root->left = build(preorder, pl + 1, pl + 1 + (pos - il - 1), il, pos - 1);
        root->right = build(preorder, pl + 1 + pos - il, pr, pos + 1, ir);
        return root;
    }
};

Leetcode114. 二叉树展开为链表

思路

每一次把左子树右链插入根节点右子树中

/**
 * 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 flatten(TreeNode* root) {
        while (root) {
            if (root->left) {
                auto p = root->left;
                while (p->right) p = p->right;
                p->right = root->right;
                root->right = root->left;
                root->left = nullptr;
            }
            root = root->right;
        }
    }
};

Leetcode121. 买股票的最佳时机

class Solution {
public:
    int maxProfit(vector<int>& prices) {
        int res = 0, minn = prices[0];
        for (int i = 1; i < prices.size(); i ++ )
        {
            res = max(res, prices[i] - minn);
            minn = min(minn, prices[i]);
        }
        return res;
    }
};

Leetcode124. 二叉树中的最大路径和

思路

DFS,dfs(u)dfs(u)dfs(u)表示以uuu结点为最高点的路径和最大值,因此返回root−>val+max(l,r)root->val+max(l,r)root>val+max(l,r),每一次递归过程中更新答案,答案应该是root−>val+l+rroot->val+l+rroot>val+l+r,最后返回答案

/**
 * 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:
    int ans = INT_MIN;
    int maxPathSum(TreeNode* root) {
        dfs(root);
        return ans;
    }

    int dfs(TreeNode* root) {
        if (!root) return 0;
        int l = max(0, dfs(root->left)), r = max(0, dfs(root->right));
        ans = max(ans, root->val + l + r);
        return root->val + max(l, r);
    }
};

Leetcode128. 最长连续序列

思路:哈希

遍历一遍数组,每一个出现的数存入哈希表中,再次枚举的时候查找连续的最大长度的区间,并且同时从哈希表中删去,避免再次被查找

class Solution {
public:
    int longestConsecutive(vector<int>& nums) {
        unordered_set<int> S;
        int res = 0;
        for (auto& t : nums) S.insert(t);
        for (auto& t : nums) {
            if (S.count(t) && S.count(t - 1) == 0) {
                int y = t;
                while (S.count(y)) {
                    S.erase(y);
                    y ++ ;
                }
                res = max(res, y - t);
            }
        }
        return res;
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Shirandexiaowo

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

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

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

打赏作者

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

抵扣说明:

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

余额充值