365天挑战LeetCode1000题——Day 073 最大二叉树 II 二叉树的右视图 路径总和 II 删除二叉搜索树中的节点

998. 最大二叉树 II

在这里插入图片描述

代码实现(自解)

/**
 * 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 {
private:
    vector<int> deconstruct(TreeNode* root) {
        vector<int> ans;
        if (!root) return ans;
        ans.push_back(root->val);
        vector<int> left = deconstruct(root->left);
        vector<int> right = deconstruct(root->right);
        ans.insert(ans.begin(), left.begin(), left.end());
        ans.insert(ans.end(), right.begin(), right.end());
        return ans;
    }

    TreeNode* construct(vector<int>& nums) {
        if (!nums.size()) return NULL;
        auto it = max_element(nums.begin(), nums.end());
        TreeNode* root = new TreeNode(*it);
        vector<int> left = {nums.begin(), it};
        vector<int> right = {it + 1, nums.end()};
        root->left = construct(left);
        root->right = construct(right);
        return root;
    }
public:
    TreeNode* insertIntoMaxTree(TreeNode* root, int val) {
        vector<int> ans = deconstruct(root);
        ans.push_back(val);
        return construct(ans);
    }
};

199. 二叉树的右视图

在这里插入图片描述

代码实现(自解)

/**
 * 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> rightSideView(TreeNode* root) {
        vector<int> ans;
        queue<TreeNode*> myQueue;
        if (!root) return ans;
        myQueue.push(root);
        while (!myQueue.empty()) {
            int sz = myQueue.size();
            while (sz--) {
                TreeNode* tmp = myQueue.front();
                myQueue.pop();
                if (tmp->left) myQueue.push(tmp->left);
                if (tmp->right) myQueue.push(tmp->right);
                if (!sz) ans.push_back(tmp->val);
            }
        }
        return ans;
    }
};

112. 路径总和

在这里插入图片描述

代码实现(自解)

/**
 * 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 state {
public:
    TreeNode* root;
    vector<int> path;
    int sum;
    state(TreeNode* root) : root(root) {};
    state(TreeNode* root, state fa) : root(root), path(fa.path) {
        path.push_back(this->root->val);
        sum = fa.sum + this->root->val;
    }
};

class Solution {
public:
    bool hasPathSum(TreeNode* root, int targetSum) {
        vector<vector<int>> ans;
        queue<state> myQueue;
        if (!root) return false;
        state root_state(root);
        root_state.path.push_back(root->val);
        root_state.sum = root->val;
        myQueue.push(root_state);
        while (!myQueue.empty()) {
            int sz = myQueue.size();
            while (sz--) {
                state tmp = myQueue.front();
                myQueue.pop();
                if (!tmp.root->left && !tmp.root->right) {
                    if (tmp.sum == targetSum) {
                        return true;
                    }
                }
                if (tmp.root->left) {
                    state left(tmp.root->left, tmp);
                    myQueue.push(left);
                }
                if (tmp.root->right) {
                    state right(tmp.root->right, tmp);
                    myQueue.push(right);
                }
            }
        }
        return false;
    }
};

113. 路径总和 II

在这里插入图片描述

代码实现(自解)

/**
 * 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 state {
public:
    TreeNode* root;
    vector<int> path;
    int sum;
    state(TreeNode* root) : root(root) {};
    state(TreeNode* root, state fa) : root(root), path(fa.path) {
        path.push_back(this->root->val);
        sum = fa.sum + this->root->val;
    }
};

class Solution {
public:
    vector<vector<int>> pathSum(TreeNode* root, int targetSum) {
        vector<vector<int>> ans;
        queue<state> myQueue;
        if (!root) return ans;
        state root_state(root);
        root_state.path.push_back(root->val);
        root_state.sum = root->val;
        myQueue.push(root_state);
        while (!myQueue.empty()) {
            int sz = myQueue.size();
            while (sz--) {
                state tmp = myQueue.front();
                myQueue.pop();
                if (!tmp.root->left && !tmp.root->right) {
                    if (tmp.sum == targetSum) {
                        ans.push_back(tmp.path);
                        continue;
                    }
                }
                if (tmp.root->left) {
                    state left(tmp.root->left, tmp);
                    myQueue.push(left);
                }
                if (tmp.root->right) {
                    state right(tmp.root->right, tmp);
                    myQueue.push(right);
                }
            }
        }
        return ans;
    }
};

450. 删除二叉搜索树中的节点

在这里插入图片描述

代码实现(自解)

/**
 * 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* deleteNode(TreeNode* root, int key) {
        if (!root) return nullptr;
        if (root->val != key) {
            root->left = deleteNode(root->left, key);
            root->right = deleteNode(root->right, key);
            return root;
        }
        else {
            if (!root->left && !root->right) return nullptr;
            if (root->left && root->right) {
                TreeNode* tmp = root->left;
                TreeNode* pre = tmp;
                if (!tmp->right) {
                    root->val = tmp->val;
                    root->left = tmp->left;
                }
                while (tmp->right) {
                    pre = tmp;
                    tmp = tmp->right;
                }
                TreeNode* tmp1 = tmp->left;
                // 左节点接到要删除的结点的父节点的右边
                pre->right = tmp1;
                // 将要删除的结点的值赋给原结点
                root->val = tmp->val;
                return root;
            }
            else {
                if (root->left) return root->left;
                if (root->right) return root->right;
            }
        }
        return root;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值