最大二叉树
和昨日的依据后序和中序序列得到前序二叉树的例子相似,利用递归算法,每个递归体中查找数组中的最大值的位置,将序列分为左右,当传入数组大小为1时,传回以这个数组唯一元素构建的结点。具体写法如下。
class Solution {
public:
TreeNode* constructMaximumBinaryTree(vector<int>& nums) {
if(nums.size() == 0){
return nullptr;
}
int index = -1;
int max = INT_MIN;
for(int i = 0; i<nums.size();i++){
if(nums[i]>max){
max = nums[i];
index = i;
}
}//找到最大值
vector<int>left(nums.begin(),nums.begin()+index);
vector<int>right(nums.begin()+index+1,nums.end());
TreeNode * root = new TreeNode(nums[index]);
if(nums.size() == 1){
TreeNode * root = new TreeNode(nums[0]);
return root;
}
root->left = constructMaximumBinaryTree(left);
root->right = constructMaximumBinaryTree(right);
return root;
}
};
优化后的写法
class Solution {
public:
TreeNode* build(vector<int>& nums, int start, int end) {
if(start == end){
return nullptr;
}
int maxIndex = start;
for(int i = start; i < end; i++){
if(nums[i] > nums[maxIndex]){
maxIndex = i;
}
}
TreeNode* root = new TreeNode(nums[maxIndex]);
root->left = build(nums, start, maxIndex);
root->right = build(nums, maxIndex + 1, end);
return root;
}
TreeNode* constructMaximumBinaryTree(vector<int>& nums) {
return build(nums, 0, nums.size());
}
};
算法的事件和空间复杂度均为O(n)。
合并二叉树
递归法
简单题,先判断两个二叉树是否存在空,若有一个为空,则返回另一个二叉树。
当两者均不为空的情况下,对左右子树进行前序遍历(从根节点开始),树1每个结点的val值加上树2每个结点的val。具体代码如下。
class Solution {
public:
TreeNode* mergeTrees(TreeNode* root1, TreeNode* root2) {
if(root1 == nullptr){
return root2;
}//当树1为空,返回树2
else if(root2 == nullptr){
return root1;
}//当树2为空,返回树1
else{
root1->val += root2->val;//树1结点的val值加上树2相应结点的val。
}
root1->left = mergeTrees(root1->left,root2->left);
root1->right = mergeTrees(root1->right,root2->right);
return root1;
}
};
算法的时间复杂度和空间复杂度为O(n)。
迭代方法
class Solution {
public:
TreeNode* mergeTrees(TreeNode* t1, TreeNode* t2) {
if (!t1) return t2;
if (!t2) return t1;
stack<pair<TreeNode*, TreeNode*>> stk;
stk.push({t1, t2});
while (!stk.empty()) {
auto p = stk.top();
stk.pop();
if (!p.first || !p.second) continue;
p.first->val += p.second->val;
if (!p.first->left) {
p.first->left = p.second->left;
} else {
stk.push({p.first->left, p.second->left});
}
if (!p.first->right) {
p.first->right = p.second->right;
} else {
stk.push({p.first->right, p.second->right});
}
}
return t1;
}
};
二叉搜索树中的搜索
递归,当当前结点与val相同或为空时,返回当前结点,当当前结点的值小于val,查询右子树,当当前结点的值大于val,查询左子树。
class Solution {
public:
TreeNode* searchBST(TreeNode* root, int val) {
if(root == nullptr||root->val == val)
return root;
else if(root->val<val){
return searchBST(root->right,val);
}
else{
return searchBST(root->left,val);
}
}
};
在二叉搜索树中查找一个结点的时间复杂度和空间复杂度取决于树的高度,当为平衡的二叉搜索树时,查找的时间复杂度和空间复杂度为O(logn),若不平衡,则最差变为一个链表,两者变为O(n)
验证二叉搜索树
考虑到结点的左子树全部小于结点,结点的右子树全部大于结点,采用左中右的中序遍历方式得到整体序列,判断序列递增情况即可验证二叉搜索树。(花了很久时间没想到只想到中序递归,唉)
class Solution {
public:
vector<long> ans;
void inorder(TreeNode*root,vector<long>&ans){
if(root == nullptr){
return;
}
inorder(root->left,ans);
ans.push_back(root->val);
inorder(root->right,ans);
}
bool isValidBST(TreeNode* root) {
inorder(root,ans);
for(int i = 1 ;i < ans.size(); i ++){
if(ans[i] < ans[i-1]){
return false;
}
}
return true;
}
};
或者直接中序遍历的时候进行比较。
class Solution {
public:
TreeNode* prev = nullptr;
bool isValidBST(TreeNode* root) {
if (root == nullptr) {
return true;
}
// 验证左子树
if (!isValidBST(root->left)) {
return false;
}
// 验证当前节点,如果前一个节点存在且当前节点的值不大于前一个节点的值,则不是BST
if (prev != nullptr && root->val <= prev->val) {
return false;
}
// 更新前一个节点
prev = root;
// 验证右子树
return isValidBST(root->right);
}
};
这个方法相比前面的方法将算法的空间复杂度降为O(1),时间复杂度还是O(n)。
迭代法
参考代码随想录
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 && cur->val <= pre->val)
return false;
pre = cur; //保存前一个访问的结点
cur = cur->right; // 右
}
}
return true;
}
};