二叉树
一、最大二叉树
思路和构造二叉树相同,1找出最大值和分割点,2切割序列,3递归
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
TreeNode* traversal(vector<int> nums) {
//终止条件
if (nums.size() == 0) return NULL;
//找出最大值和切割点
int maxValue = 0;
int index;
for (int i = 0; i < nums.size(); i++) {
if (nums[i] > maxValue) {
maxValue = nums[i];
index = i;
}
}
TreeNode* root = new TreeNode(maxValue);
if (nums.size() == 1) return root;
//切割
vector<int>leftNums(nums.begin(), nums.begin() + index);
vector<int>rightNums(nums.begin() + index + 1, nums.end());
//连接
root->left = traversal(leftNums);
root->right = traversal(rightNums);
return root;
}
TreeNode* constructMaximumBinaryTree(vector<int>& nums) {
if (nums.size() == 0) return NULL;
return traversal(nums);
}
};
二、合并二叉树
两个指针同时移动,如果有一个不存在,即返回另一个。两个都存在,将数值相加到root1
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
//递归,两个指针,同时移动
TreeNode* traversal(TreeNode* root1, TreeNode* root2) {
//终止条件
if (root1 == nullptr && root2 == nullptr) return nullptr;
//中
if (root1 == nullptr) return root2;
if (root2 == nullptr) return root1;
root1->val += root2->val;
root1->left = traversal(root1->left, root2->left);
root1->right = traversal(root1->right, root2->right);
return root1;
}
TreeNode* mergeTrees(TreeNode* root1, TreeNode* root2) {
if (!root1 && !root2) return NULL;
return traversal(root1, root2);
}
};
三、二叉搜索树中的搜索
相较于普通二叉树,不用全局搜索,只需要根据大小向左右移动
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
//递归
TreeNode* searchBST(TreeNode* root, int val) {
if (root == nullptr) return NULL;
if (root->val == val) return root;
TreeNode* result = NULL;
if (root->val > val) result = searchBST(root->left, val);
if (root->val < val) result = searchBST(root->right, val);
return result;
}
};
四、验证二叉搜索树
使用数组最为简单直接
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
vector<int>nums;
void traversal(TreeNode* root) {
if (root == nullptr) return;
traversal(root->left);
nums.push_back(root->val);
traversal(root->right);
}
bool isValidBST(TreeNode* root) {
if (root == nullptr) return false;
traversal(root);
for (int i = 1; i < nums.size(); i++) {
if (nums[i] <= nums[i - 1]) return false;
}
return true;
}
};
总结
构造二叉树还得多写
学习时间90min。
学习资料:《代码随想录》。