998. 最大二叉树 II
代码实现(自解)
class Solution {
private:
vector<int> deconstruct(TreeNode* root) {
vector<int> ans;
if (!root) return ans;
ans.push_back(root->val);
vector<int> left = deconstruct(root->left);
vector<int> right = deconstruct(root->right);
ans.insert(ans.begin(), left.begin(), left.end());
ans.insert(ans.end(), right.begin(), right.end());
return ans;
}
TreeNode* construct(vector<int>& nums) {
if (!nums.size()) return NULL;
auto it = max_element(nums.begin(), nums.end());
TreeNode* root = new TreeNode(*it);
vector<int> left = {nums.begin(), it};
vector<int> right = {it + 1, nums.end()};
root->left = construct(left);
root->right = construct(right);
return root;
}
public:
TreeNode* insertIntoMaxTree(TreeNode* root, int val) {
vector<int> ans = deconstruct(root);
ans.push_back(val);
return construct(ans);
}
};
199. 二叉树的右视图
代码实现(自解)
class Solution {
public:
vector<int> rightSideView(TreeNode* root) {
vector<int> ans;
queue<TreeNode*> myQueue;
if (!root) return ans;
myQueue.push(root);
while (!myQueue.empty()) {
int sz = myQueue.size();
while (sz--) {
TreeNode* tmp = myQueue.front();
myQueue.pop();
if (tmp->left) myQueue.push(tmp->left);
if (tmp->right) myQueue.push(tmp->right);
if (!sz) ans.push_back(tmp->val);
}
}
return ans;
}
};
112. 路径总和
代码实现(自解)
class state {
public:
TreeNode* root;
vector<int> path;
int sum;
state(TreeNode* root) : root(root) {};
state(TreeNode* root, state fa) : root(root), path(fa.path) {
path.push_back(this->root->val);
sum = fa.sum + this->root->val;
}
};
class Solution {
public:
bool hasPathSum(TreeNode* root, int targetSum) {
vector<vector<int>> ans;
queue<state> myQueue;
if (!root) return false;
state root_state(root);
root_state.path.push_back(root->val);
root_state.sum = root->val;
myQueue.push(root_state);
while (!myQueue.empty()) {
int sz = myQueue.size();
while (sz--) {
state tmp = myQueue.front();
myQueue.pop();
if (!tmp.root->left && !tmp.root->right) {
if (tmp.sum == targetSum) {
return true;
}
}
if (tmp.root->left) {
state left(tmp.root->left, tmp);
myQueue.push(left);
}
if (tmp.root->right) {
state right(tmp.root->right, tmp);
myQueue.push(right);
}
}
}
return false;
}
};
113. 路径总和 II
代码实现(自解)
class state {
public:
TreeNode* root;
vector<int> path;
int sum;
state(TreeNode* root) : root(root) {};
state(TreeNode* root, state fa) : root(root), path(fa.path) {
path.push_back(this->root->val);
sum = fa.sum + this->root->val;
}
};
class Solution {
public:
vector<vector<int>> pathSum(TreeNode* root, int targetSum) {
vector<vector<int>> ans;
queue<state> myQueue;
if (!root) return ans;
state root_state(root);
root_state.path.push_back(root->val);
root_state.sum = root->val;
myQueue.push(root_state);
while (!myQueue.empty()) {
int sz = myQueue.size();
while (sz--) {
state tmp = myQueue.front();
myQueue.pop();
if (!tmp.root->left && !tmp.root->right) {
if (tmp.sum == targetSum) {
ans.push_back(tmp.path);
continue;
}
}
if (tmp.root->left) {
state left(tmp.root->left, tmp);
myQueue.push(left);
}
if (tmp.root->right) {
state right(tmp.root->right, tmp);
myQueue.push(right);
}
}
}
return ans;
}
};
450. 删除二叉搜索树中的节点
代码实现(自解)
class Solution {
public:
TreeNode* deleteNode(TreeNode* root, int key) {
if (!root) return nullptr;
if (root->val != key) {
root->left = deleteNode(root->left, key);
root->right = deleteNode(root->right, key);
return root;
}
else {
if (!root->left && !root->right) return nullptr;
if (root->left && root->right) {
TreeNode* tmp = root->left;
TreeNode* pre = tmp;
if (!tmp->right) {
root->val = tmp->val;
root->left = tmp->left;
}
while (tmp->right) {
pre = tmp;
tmp = tmp->right;
}
TreeNode* tmp1 = tmp->left;
pre->right = tmp1;
root->val = tmp->val;
return root;
}
else {
if (root->left) return root->left;
if (root->right) return root->right;
}
}
return root;
}
};