654.最大二叉树
文章讲解:代码随想录 (programmercarl.com)
视频讲解:又是构造二叉树,又有很多坑!| LeetCode:654.最大二叉树
题目链接:654. 最大二叉树 - 力扣(LeetCode)
题目: 给定一个不含重复元素的整数数组。一个以此数组构建的最大二叉树定义如下:
- 二叉树的根是数组中的最大元素。
- 左子树是通过数组中最大值左边部分构造出的最大二叉树。
- 右子树是通过数组中最大值右边部分构造出的最大二叉树。
class Solution {
public:
TreeNode* traversal (vector<int>& nums) {
TreeNode* node = new TreeNode();
if (nums.size() == 1) {
node->val = nums[0];
return node;
}
//找最大值
int maxValue = INT_MIN;
int maxIndex = 0;
for (int i = 0; i < nums.size(); i++) {
if (maxValue < nums[i]) {
maxValue = nums[i];
maxIndex = i;
}
}
node->val = maxValue;
//分割左右子树
if (maxIndex > 0) {
vector<int> newVec(nums.begin(), nums.begin() + maxIndex);
node->left = traversal(newVec);
}
if (maxIndex < nums.size() - 1) {
vector<int> newVec(nums.begin() + maxIndex + 1, nums.end());
node->right = traversal(newVec);
}
return node;
}
TreeNode* constructMaximumBinaryTree(vector<int>& nums) {
return traversal(nums);
}
};
617.合并二叉树
文章讲解:代码随想录 (programmercarl.com)
视频讲解:
题目链接:617. 合并二叉树 - 力扣(LeetCode)
题目: 给定两个二叉树,想象当你将它们中的一个覆盖到另一个上时,两个二叉树的一些节点便会重叠。
你需要将他们合并为一个新的二叉树。合并的规则是如果两个节点重叠,那么将他们的值相加作为节点合并后的新值,否则不为 NULL 的节点将直接作为新二叉树的节点。
递归
class Solution {
public:
TreeNode* mergeTrees(TreeNode* root1, TreeNode* root2) {
if (root1 == NULL) return root2;
if (root2 == NULL) return root1;
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 == NULL) return root2;
if (root2 == NULL) return root1;
queue<TreeNode*> que;
que.push(root1);
que.push(root2);
while (!que.empty()) {
TreeNode* node1 = que.front();
que.pop();
TreeNode* node2 = que.front();
que.pop();
node1->val += node2->val;
if (node1->left && node2->left) {
que.push(node1->left);
que.push(node2->left);
}
if (node1->right && node2->right) {
que.push(node1->right);
que.push(node2->right);
}
if (!node1->left && node2->left) {
node1->left = node2->left;
}
if (!node1->right && node2->right) {
node1->right = node2->right;
}
}
return root1;
}
};
700.二叉搜索树中的搜索
文章讲解:代码随想录 (programmercarl.com)
视频讲解:不愧是搜索树,这次搜索有方向了!| LeetCode:700.二叉搜索树中的搜索
题目链接: 700. 二叉搜索树中的搜索 - 力扣(LeetCode)
题目: 给定二叉搜索树(BST)的根节点和一个值。 你需要在BST中找到节点值等于给定值的节点。 返回以该节点为根的子树。 如果节点不存在,则返回 NULL。
递归
class Solution {
public:
TreeNode* searchBST(TreeNode* root, int val) {
if (root == NULL || root->val == val) return root;
TreeNode* result;
if (root->val > val) result = searchBST(root->left, val);
if (root->val < val) result = searchBST(root->right, val);
return result;
}
};
迭代
class Solution {
public:
TreeNode* searchBST(TreeNode* root, int val) {
while (root != NULL) {
if (root->val > val) root = root->left;
else if (root->val < val) root = root->right;
else return root;
}
return NULL;
}
};
98.验证二叉搜索树
文章讲解:代码随想录 (programmercarl.com)
视频讲解:你对二叉搜索树了解的还不够! | LeetCode:98.验证二叉搜索树
题目链接:98. 验证二叉搜索树 - 力扣(LeetCode)
题目: 给定一个二叉树,判断其是否是一个有效的二叉搜索树。
假设一个二叉搜索树具有如下特征:
- 节点的左子树只包含小于当前节点的数。
- 节点的右子树只包含大于当前节点的数。
- 所有左子树和右子树自身必须也是二叉搜索树。
递归(数组)
class Solution {
public:
vector<int> vec;
void traversal(TreeNode* root) {
if (root == NULL) return;
traversal(root->left);
vec.push_back(root->val);
traversal(root->right);
}
bool isValidBST(TreeNode* root) {
vec.clear();
traversal(root);
for (int i = 1; i < vec.size(); i++) {
if (vec[i] <= vec[i - 1]) return false;
}
return true;
}
};
递归
class Solution {
public:
TreeNode* pre = NULL;
bool isValidBST(TreeNode* root) {
if (root == NULL) return true;
bool left = isValidBST(root->left);
if (pre != NULL && pre->val >= root->val) return false;
pre = root;
bool right = isValidBST(root->right);
return left && right;
}
};
递归
class Solution {
public:
bool isValidBST(TreeNode* root) {
stack<TreeNode*> st;
TreeNode* cur = root;
TreeNode* pre = NULL;
while (cur != NULL || !st.empty()) {
if (cur != NULL) {
st.push(cur);
cur = cur->left;
}else {
cur = st.top();
st.pop();
if (pre != NULL && pre->val >= cur->val) return false;
pre = cur;
cur = cur -> right;
}
}
return true;
}
};