二叉树part06 654.最大二叉树 617.合并二叉树 700.二叉搜索树中的搜索 98.验证二叉搜索树
654. 最大二叉树
方法一:递归
class Solution {
public:
TreeNode* constructMaximumBinaryTree(vector<int>& nums) {
if(nums.size()==1) return new TreeNode(nums[0]);
int maxValue =0;
int Index =0;
for(int i=0;i<nums.size();i++)
{
if(nums[i]>maxValue){
maxValue=nums[i];
Index = i;
}
}
TreeNode* node = new TreeNode(maxValue);
if(Index>0){
vector<int> newVec(nums.begin(),nums.begin()+Index);
node->left = constructMaximumBinaryTree(newVec);
}
if(Index<nums.size()-1)
{
vector<int> newVec(nums.begin()+Index+1,nums.end());
node->right = constructMaximumBinaryTree(newVec);
}
return node;
}
};
方法二:递归优化
class Solution {
public:
TreeNode* traversal(vector<int>& nums, int left, int right) {
if(left>=right) return nullptr;
int maxValue =nums[left];
int maxValueIndex =left;
for (int i = left+1; i < right; i++) {
if (nums[i] > maxValue) {
maxValue = nums[i];
maxValueIndex = i;
}
}
TreeNode* node = new TreeNode(maxValue);
node->left = traversal(nums,left,maxValueIndex);
node->right = traversal(nums,maxValueIndex+1,right);
return node;
}
TreeNode* constructMaximumBinaryTree(vector<int>& nums) {
return traversal(nums,0,nums.size());
}
};
617. 合并二叉树
方法一:递归
class Solution {
public:
TreeNode* mergeTrees(TreeNode* root1, TreeNode* root2) {
if(root1 == nullptr) return root2;
if(root2 == nullptr) 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==nullptr) return root2;
if(root2==nullptr) 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!=nullptr&&node2->left!=nullptr)
{
que.push(node1->left);
que.push(node2->left);
}
if(node1->right!=nullptr&&node2->right!=nullptr)
{
que.push(node1->right);
que.push(node2->right);
}
if(node1->left==nullptr&&node2->left!=nullptr) node1->left = node2->left;
if(node1->right==nullptr&&node2->right!=nullptr) node1->right = node2->right;
}
return root1;
}
};
700. 二叉搜索树中的搜索
方法一:递归
class Solution {
public:
TreeNode* searchBST(TreeNode* root, int val) {
if(root ==nullptr || root->val == val) return root;
TreeNode* result = nullptr;
if(val<root->val) result = searchBST(root->left,val);
if(val>root->val) result = searchBST(root->right,val);
return result;
}
};
方法二:迭代
class Solution {
public:
TreeNode* searchBST(TreeNode* root, int val) {
while(root!=nullptr)
{
if(val<root->val) root = root->left;
else if(val>root->val) root = root->right;
else return root;
}
return NULL;
}
};
98. 验证二叉搜索树
方法一:中序遍历存到数组,判断数组是否递增
class Solution {
private:
vector<int> vec;
void traversal(TreeNode* root) {
if (root == NULL) return;
traversal(root->left);
vec.push_back(root->val);
traversal(root->right);
}
public:
bool isValidBST(TreeNode* root) {
traversal(root);
for(int i =1; i<vec.size();i++)
{
if(vec[i]<vec[i-1]) return false;
}
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;
}
};
方法三:递归法(双指针优化)
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;
}
};