二叉树练习5

654. 最大二叉树

方法:递归

做过106,105后这个题就很简单

class Solution {
public:
    TreeNode* constructMaximumBinaryTree(vector<int>& nums) {
        TreeNode* root = new TreeNode(nums[0]);
        if (nums.size() == 1) return root;
        int maxindex = 0;
        int maxval = 0;
        for (int i = 0; i < nums.size(); i++) {
            if (nums[i] > maxval) {
                maxindex = i;
                maxval = nums[i];
            }
        }
        root->val = maxval;
        if (maxindex > 0) {
            vector<int> leftv(nums.begin(), nums.begin() + maxindex);
            root->left = constructMaximumBinaryTree(leftv);
        }
        if (maxindex < nums.size() - 1) {
            vector<int> rightv(nums.begin() + maxindex+1, nums.end());
            root->right = constructMaximumBinaryTree(rightv);
        }
        return root;
    }
};

优化则是不用每次新开数组,移动下标就行,在此省略

617. 合并二叉树

方法:递归

前序(中序,后序都是可以的)
1.新建二叉树写法:

class Solution {
public:
    TreeNode* mergeTrees(TreeNode* root1, TreeNode* root2) {
        if (root1 == nullptr && root2 == nullptr) return nullptr;//这一步包含到下一步中,可以不写
        if (root1 == nullptr || root2 == nullptr) return root1 ? root1 : 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;
    }
};

2.改变原来二叉树写法:

class Solution {
public:
    TreeNode* mergeTrees(TreeNode* root1, TreeNode* root2) {
        if (root1 == nullptr || root2 == nullptr) return root1 ? root1 : root2;
        root1->val += root2->val;
        root1->left = mergeTrees(root1->left, root2->left);
        root1->right = mergeTrees(root1->right, root2->right);
        return root1;
    }
};

方法:迭代

类型101. 对称二叉树的处理,把两棵树的结点同时加入队列。
尽量使用原来树的结点修改,不然下面的while里边的if判断会多两种情况

class Solution {
public:
    TreeNode* mergeTrees(TreeNode* root1, TreeNode* root2) {
        if (!root1 || !root2) return root1 ? root1 : root2;
        queue<TreeNode*> q;
        q.push(root1);
        q.push(root2);
        while (!q.empty()) {
            TreeNode* node1 = q.front();q.pop();
            TreeNode* node2 = q.front();q.pop();
            node1->val += node2->val;
            if (node1->left != nullptr && node2->left != nullptr) {
                q.push(node1->left);
                q.push(node2->left);
            }
            if (node1->right != nullptr && node2->right != nullptr) {
                q.push(node1->right);
                q.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;
        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. 验证二叉搜索树

方法:递归

二叉搜索树的特性,就是如果中序遍历该树,则结点的值是递增的,利用双指针判断是否递增即可

class Solution {
private:
    TreeNode* pre = nullptr;
public:
    bool isValidBST(TreeNode* root) {
        if (root == nullptr) return true;
        bool left = isValidBST(root->left);
        if (pre != nullptr && pre->val >= root->val) return false;
        pre = root;
        bool right = isValidBST(root->right);
        return left && right;
    }
};

方法:迭代

利用栈模拟递归,中序遍历的模板题
详细介绍可看二叉树三种遍历的迭代写法二叉树练习1

class Solution {
public:
    bool isValidBST(TreeNode* root) {
        stack<TreeNode*> st;
        TreeNode* cur = root;
        TreeNode* pre = nullptr;
        while (cur || !st.empty()) {
            if (cur) {
                st.push(cur);
                cur = cur->left;
            } else {
                cur = st.top();
                st.pop();
                if (pre && cur->val <= pre->val) return false;
                pre = cur;
                cur = cur->right;
            }
        }
        return true;
    }
};
  • 9
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值