654.最大二叉树
654.最大二叉树
利用构造二叉树的思想,前序遍历,构造中间节点,在构造左右子树的vector容器,递归遍历左右子树
class Solution {
public:
TreeNode* constructMaximumBinaryTree(vector<int>& nums) {
if (nums.size() == 0) {
return nullptr;
}
// 找到最大值及最大值的下标
int maxValIndex = 0;
int maxVal = nums[0];
for (int i = 0; i < nums.size(); ++i) {
if (nums[i] > maxVal) {
maxVal = nums[i];
maxValIndex = i;
}
}
TreeNode* root = new TreeNode(maxVal);
// 左子树 [0, maxValIndex)
vector<int> leftNums(nums.begin(), nums.begin() + maxValIndex);
// 右子树 [maxValIndex + 1, end)
vector<int> rightNums(nums.begin() + maxValIndex + 1, nums.end());
root->left = constructMaximumBinaryTree(leftNums);
root->right = constructMaximumBinaryTree(rightNums);
return root;
}
};
不构造左右子树的数组,直接使用区间左右端点
class Solution {
public:
TreeNode* traversal(vector<int>& nums, int left, int right) {
if (right - left == 0) {
return nullptr;
}
// 找到最大值及最大值的下标
int maxValIndex = left;
int maxVal = nums[left];
for (int i = left; i < right; ++i) {
if (nums[i] > maxVal) {
maxVal = nums[i];
maxValIndex = i;
}
}
TreeNode* root = new TreeNode(maxVal);
// 左子树 [left, maxValIndex)
root->left = traversal(nums, left, maxValIndex);
// 右子树 [maxValIndex + 1, right)
root->right = traversal(nums, maxValIndex + 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 && root2 == nullptr) {
return nullptr;
} else if (root1 != nullptr && root2 == nullptr) {
return root1;
} else if (root1 == nullptr && root2 != nullptr) {
return root2;
}
TreeNode* root = new TreeNode(root1->val + root2->val);
root->left = mergeTrees(root1->left, root2->left);
root->right = mergeTrees(root1->right, root2->right);
return root;
}
};
700.二叉搜索树中的搜索
给定二叉搜索树(BST)的根节点 root 和一个整数值 val。
你需要在 BST 中找到节点值等于 val 的节点。 返回以该节点为根的子树。 如果节点不存在,则返回 null 。
二叉搜索树有数值的有序树;若它的左子树不空,则左子树上的所有节点的值均小于它的根节点的值;若它的右子树不空,则右子树上的所有节点的值均大于它的根节点的值;它的左右子树也分别是二叉搜索树;
递归
class Solution {
public:
TreeNode* searchBST(TreeNode* root, int val) {
if (root == nullptr || root->val == val) {
return root;
}
if (root->val > val) {
return searchBST(root->left, val);
}
if (root->val < val) {
return searchBST(root->right, val);
}
return nullptr;
}
};
迭代
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.验证二叉搜索树
给你一个二叉树的根节点 root ,判断其是否是一个有效的二叉搜索树。
有效 二叉搜索树定义如下:
节点的左子树 只包含 小于 当前节点的数。
节点的右子树只包含 大于 当前节点的数。
所有左子树和右子树自身必须也是二叉搜索树。
中序遍历的结果是递增的;
class Solution {
public:
bool isValidBST(TreeNode* root) {
// int preVal = INT_MIN; // 存储前一个节点的值 测试用例中右int最小值
long long preVal = LONG_MIN;
stack<TreeNode*> st;
TreeNode* cur = root;
while (cur != NULL || !st.empty()) {
if (cur != NULL) {
st.push(cur);
cur = cur->left;
} else {
cur = st.top();
st.pop();
if (cur->val <= preVal) {
return false;
}
preVal = cur->val;
cur = cur->right;
}
}
return true;
}
};
class Solution {
public:
long long maxVal = LONG_MIN;
bool isValidBST(TreeNode* root) {
if (root == NULL) return true;
bool left = isValidBST(root->left);
// 中序遍历,验证遍历的元素是不是从小到大
if (maxVal < root->val) maxVal = root->val;
else return false;
bool right = isValidBST(root->right);
return left && right;
}
};