Day 20 二叉树
654. 最大二叉树
递归
class Solution {
TreeNode *build(const vector<int> &nums, int left, int right)
{
if (left >= right) return nullptr;
int idx = left;
for (int i = left + 1; i < right; ++i)
{
if (nums[i] > nums[idx])
{
idx = i;
}
}
TreeNode *root = new TreeNode(nums[idx]);
root->left = build(nums, left, idx);
root->right = build(nums, idx + 1, right);
return root;
}
public:
TreeNode* constructMaximumBinaryTree(vector<int>& nums) {
return build(nums, 0, nums.size());
}
};
617. 合并二叉树
递归
class Solution {
public:
TreeNode* mergeTrees(TreeNode* root1, TreeNode* root2) {
if (!root1 && !root2) return nullptr;
else if (!root1) return root2;
else if (!root2) return root1;
root1->val = root1->val + root2->val;
root1->left = mergeTrees(root1->left, root2->left);
root1->right = mergeTrees(root1->right, root2->right);
return root1;
}
};
迭代
class Solution {
public:
TreeNode* mergeTrees(TreeNode* root1, TreeNode* root2) {
if (!root1) return root2;
if (!root2) return root1;
queue<TreeNode*> que;
que.push(root1);
que.push(root2);
TreeNode *cur1, *cur2;
while (!que.empty())
{
cur1 = que.front();
que.pop();
cur2 = que.front();
que.pop();
cur1->val = cur1->val + cur2->val;
if (cur1->left && cur2->left)
{
que.push(cur1->left);
que.push(cur2->left);
}
if (cur1->right && cur2->right)
{
que.push(cur1->right);
que.push(cur2->right);
}
if (!cur1->left && cur2->left)
{
cur1->left = cur2->left;
}
if (!cur1->right && cur2->right)
{
cur1->right = cur2->right;
}
}
return root1;
}
};
700. 二叉搜索树中的搜索
递归
class Solution {
public:
TreeNode* searchBST(TreeNode* root, int val) {
if (!root) return nullptr;
if (root->val == val) return root;
if (root->val < val) return searchBST(root->right, val);
else return searchBST(root->left, val);
}
};
迭代
class Solution {
public:
TreeNode* searchBST(TreeNode* root, int val) {
TreeNode *cur = root;
while (cur)
{
if (cur->val > val)
{
cur = cur->left;
}
else if (cur->val < val)
{
cur = cur->right;
}
else
{
return cur;
}
}
return nullptr;
}
};
98. 验证二叉搜索树
遇到两个坑的地方:文章来源:https://www.toymoban.com/news/detail-600850.html
- 递归的时候,不能光验证左子树的根节点小于当前根节点,右子树的根节点大于但当前根节点,别忘了左子树上的每一个节点都要求比根节点要小,右子树上的每一个节点都要求比根节点大。
- 根节点跟左(右)子树中的某一个节点相等,也是不符合二叉搜索树的规则的
递归
用记录前一个节点的方法来处理文章来源地址https://www.toymoban.com/news/detail-600850.html
class Solution {
TreeNode *pre = nullptr;
bool flag = true;
void tracking(TreeNode *root)
{
if (!root) return;
tracking(root->left);
if (pre)
{
if (root->val <= pre->val) // 注意:这里是等号,两值相等也不符合
{
flag = false;
}
}
pre = root;
tracking(root->right);
}
public:
bool isValidBST(TreeNode* root) {
tracking(root);
return flag;
}
};
迭代
class Solution {
public:
bool isValidBST(TreeNode* root) {
stack<TreeNode*> stk;
vector<int> table;
TreeNode *cur = root;
while (cur || !stk.empty())
{
if (cur)
{
stk.push(cur);
cur = cur->left;
}
else
{
cur = stk.top();
stk.pop();
table.push_back(cur->val);
cur = cur->right;
}
}
for (int i = 1; i < table.size(); ++i)
{
if (table[i] <= table[i - 1])
{
return false;
}
}
return true;
}
};
到了这里,关于算法刷题Day 20 最大二叉树+合并二叉树+二叉搜索树中的搜索+验证二叉搜索树的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!