代码随想录算法训练营第二十天(十九天休息) | 654. 最大二叉树、617. 合并二叉树、700. 二叉搜索树中的搜索、98. 验证二叉搜索树
654. 最大二叉树
题目
解法
- 未优化 : 效率有待提高
class Solution {
public:
TreeNode* traversal(vector<int>& nums) {
// 终止条件
TreeNode* root = new TreeNode(0); // new 创建对象并返回指针
if (nums.size() == 1) {
root->val = nums[0];
return root;
}
// 单层递归逻辑
// 确定最大值并分割区间
int maxValue;
int maxValueIdx = 0;
for(int i = 0; i < nums.size(); i++) {
if (nums[i] > maxValue) {
maxValue = nums[i];
maxValueIdx = i;
}
}
root->val = maxValue;
// 左区间
if(maxValueIdx > 0) { // 为了不把空节点带入递归,至少还有一个值传入下一个递归
vector<int> leftnums(nums.begin(), nums.begin() + maxValueIdx);
root->left = traversal(leftnums);
}
//右区间
if (maxValueIdx < nums.size() - 1) { // 为了不把空节点带入递归
vector<int> rightnums(nums.begin() + maxValueIdx + 1, nums.end());
root->right = traversal(rightnums);
}
return root;
}
TreeNode* constructMaximumBinaryTree(vector<int>& nums) {
return traversal(nums);
}
};
2.优化后
class Solution {
public:
// 对数组继续操作
TreeNode* traversal(vector<int>& nums, int left, int right) {
// 终止条件
TreeNode* root = new TreeNode(0); // new 创建对象并返回指针
if (left >= right) return nullptr;
// 单层递归逻辑
// 确定最大值并分割区间
int maxValueIdx = left;
for(int i = left + 1; i < right; i++) {
if (nums[i] > nums[maxValueIdx]) {
maxValueIdx = i;
}
}
root->val = nums[maxValueIdx];
// 左区间
root->left = traversal(nums, left, maxValueIdx);
//右区间
root->right = traversal(nums, maxValueIdx + 1, right);
return root;
}
TreeNode* constructMaximumBinaryTree(vector<int>& nums) {
return traversal(nums, 0, nums.size());
}
};
617. 合并二叉树
题目
解法
class Solution {
public:
TreeNode* mergeTrees(TreeNode* root1, TreeNode* root2) {
if (root1 == nullptr) return root2;
if (root2 == nullptr) return root1;
root1->val += root2->val;
root1->left = mergeTrees(root1->left, root2->left);
root1->right = mergeTrees(root1->right, root2->right);
return root1;
}
};
700. 二叉搜索树中的搜索
题目
解法
- 递归
class Solution {
public:
TreeNode* searchBST(TreeNode* root, int val) {
if(root == nullptr || root->val == val) return root;
TreeNode* result=nullptr;
if(root->val > val) result = searchBST(root->left, val);
if(root->val < val) result = searchBST(root->right, val);
return result;
}
};
2.迭代
class Solution {
public:
TreeNode* searchBST(TreeNode* root, int val) {
while (root != nullptr) {
if (root->val > val) root = root->left;
else if (root->val < val) root = root->right;
else return root;
}
return nullptr;
}
};
98. 验证二叉搜索树
题目
解法
- 初始想法:没有考虑左子树值均小于中间节点值
class Solution {
public:
bool isValidBST(TreeNode* root) {
if (root == nullptr) return true;
if (root->left != nullptr && root->left->val >= root->val) return false;
if (root->right != nullptr && root->right->val <= root->val) return false;
bool leftnode = isValidBST(root->left);
bool rightnode = isValidBST(root->right);
return leftnode && rightnode;
}
};
2.递归
class Solution {
public:
long long minVal = LONG_MIN;
bool isValidBST(TreeNode* root) {
if (root == nullptr) return true;
bool leftnode = isValidBST(root->left);
if (minVal < root->val) minVal = root->val;
else return false;
bool rightnode = isValidBST(root->right);
return leftnode && rightnode;
}
};
感悟
还是自己不看题解做一遍才有效