654.最大二叉树
// 最大二叉树
#include <vector>
#include <iostream>
using namespace std;
struct TreeNode
{
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};
TreeNode *coustructMaxTree(vector<int> nums)
{
TreeNode *node = new TreeNode(0);
if (nums.size() == 1)
{
node->val = nums[0];
return node;
}
int maxValue = 0;
int maxIndex = 0;
for (int i = 0; i < nums.size(); i++)
{
if (nums[i] > maxValue)
{
maxValue = nums[i];
maxIndex = i;
}
}
node->val = maxValue;
if (maxIndex > 0)
{
vector<int> newvec(nums.begin(), nums.begin() + maxIndex);
node->left = coustructMaxTree(newvec);
}
if (maxIndex < (nums.size() - 1))
{
vector<int> newvec(nums.begin() + maxIndex + 1, nums.end());
node->right = coustructMaxTree(newvec);
}
return node;
}
void printPreorder(TreeNode *node)
{
if (node == nullptr)
{
return; // 如果节点为空,则返回
}
std::cout << node->val << " "; // 首先打印根节点的值
printPreorder(node->left); // 递归打印左子树
printPreorder(node->right); // 递归打印右子树
}
int main()
{
std::vector<int> nums = {3, 2, 1, 6, 0, 5};
TreeNode *root = coustructMaxTree(nums);
printPreorder(root);
std::cout << std::endl;
return 0;
}
617.合并二叉树
// 合并二叉树
#include <iostream>
struct TreeNode
{
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};
TreeNode *mergeTrees(TreeNode *t1, TreeNode *t2)
{
if (t1 == NULL)
return t2;
if (t2 == NULL)
return t1;
TreeNode *root = new TreeNode(0);
root->val = t1->val + t2->val;
root->left = mergeTrees(t1->left, t2->left);
root->right = mergeTrees(t1->right, t2->right);
return root;
}
void printpreorder(TreeNode *root)
{
if (root == NULL)
return;
std::cout << root->val << ' ';
printpreorder(root->left);
printpreorder(root->right);
}
int main()
{
TreeNode *t1 = new TreeNode(1);
t1->left = new TreeNode(3);
t1->left->left = new TreeNode(5);
t1->right = new TreeNode(2);
TreeNode *t2 = new TreeNode(2);
t2->left = new TreeNode(1);
t2->left->right = new TreeNode(4);
t2->right = new TreeNode(3);
t2->right->right = new TreeNode(7);
TreeNode *root = mergeTrees(t1, t2);
printpreorder(root);
return 0;
}
700.二叉搜索树中的搜索
// 二叉搜索树的搜索
#include <iostream>
struct TreeNode
{
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};
TreeNode *searchBST(TreeNode *root, int val)
{
if (root == NULL || root->val == val)
return root;
TreeNode *res = NULL;
if (root->val > val)
res = searchBST(root->left, val);
if (root->val < val)
res = searchBST(root->right, val);
return res;
}
void printpreorder(TreeNode *root)
{
if (root == NULL)
return;
std::cout << root->val << ' ';
printpreorder(root->left);
printpreorder(root->right);
}
int main()
{
TreeNode *t2 = new TreeNode(4);
t2->left = new TreeNode(2);
t2->right = new TreeNode(7);
t2->left->left = new TreeNode(1);
t2->left->right = new TreeNode(3);
TreeNode *root = searchBST(t2, 2);
printpreorder(root);
return 0;
}
98.验证二叉搜索树
// 验证二叉搜索树,中序遍历判断是否有序即可
#include <iostream>
#include <vector>
using namespace std;
struct TreeNode
{
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};
void traversal(TreeNode *root, vector<int> &vec)
{
if (root == NULL)
return;
traversal(root->left, vec);
vec.push_back(root->val);
traversal(root->right, vec);
}
bool isValidBST(TreeNode *root)
{
vector<int> vec;
traversal(root, vec);
for (int i = 0; i < vec.size() - 1; i++)
{
if (vec[i] >= vec[i + 1])
return false;
}
return true;
}
int main()
{
TreeNode *t2 = new TreeNode(2);
t2->left = new TreeNode(1);
t2->right = new TreeNode(3);
isValidBST(t2) ? cout << "true" << endl : cout << "false" << endl;
return 0;
}