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

被折叠的 条评论
为什么被折叠?



