代码随想录算法训练营第二十天| 654.最大二叉树、617.合并二叉树、700.二叉搜索树中的搜索、98.验证二叉搜索树
654.最大二叉树
/**
* 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:
TreeNode* traversal(const vector<int>& nums, int begin, int end) {
if (begin == end) return nullptr;
int maxIndex = begin;
for (int i = begin + 1; i < end; i++) {
if (nums[maxIndex] < nums[i]) {
maxIndex = i;
}
}
TreeNode* root = new TreeNode(nums[maxIndex]);
root->left = traversal(nums, begin, maxIndex);
root->right = traversal(nums, maxIndex + 1, end);
return root;
}
TreeNode* constructMaximumBinaryTree(vector<int>& nums) {
return traversal(nums, 0, nums.size());
}
};
617.合并二叉树
/**
* 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:
TreeNode* searchBST(TreeNode* root, int val) {
while (root) {
if (root->val == val)
return root;
else if (root->val > val)
root = root->left;
else
root = root->right;
}
return nullptr;
}
};
700.二叉搜索树中的搜索
/**
* 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 traversal(TreeNode* node, vector<int> path, vector<vector<int>>& paths, const int& targetSum) {
path.push_back(node->val);
if (!node->left && !node->right) {
int sum = 0;
for (int i = 0; i < path.size(); i++) sum += path[i];
if (sum == targetSum) paths.push_back(path);
}
if (node->left) traversal(node->left, path, paths, targetSum);
if (node->right) traversal(node->right, path, paths, targetSum);
}
vector<vector<int>> pathSum(TreeNode* root, int targetSum) {
if (!root) return {};
vector<vector<int>> ans;
traversal(root, {}, ans, targetSum);
return ans;
}
};
98.验证二叉搜索树
/**
* 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:
bool isValidBST(TreeNode* root) {
stack<TreeNode*> inorder;
vector<int> rec;
while (root || !inorder.empty()) {
if (root) {
inorder.push(root);
root = root->left;
} else {
root = inorder.top(); inorder.pop();
rec.push_back(root->val);
root = root->right;
}
}
for (int i = 0; i < rec.size() - 1; i++) {
if (rec[i] >= rec[i + 1])
return false;
}
return true;
}
};