代码随想录算法训练营第二十二天| 235. 二叉搜索树的最近公共祖先、701.二叉搜索树中的插入操作、450.删除二叉搜索树中的节点

代码随想录算法训练营第二十二天| 235. 二叉搜索树的最近公共祖先、701.二叉搜索树中的插入操作、450.删除二叉搜索树中的节点

  • 235. 二叉搜索树的最近公共祖先
  • 701.二叉搜索树中的插入操作
  • 450.删除二叉搜索树中的节点

235. 二叉搜索树的最近公共祖先

题目链接
文章讲解

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */

class Solution {
public:
    TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
        int minVal = min(p->val, q->val), maxVal = max(p->val, q->val);
        if (root->val > maxVal)
            return lowestCommonAncestor(root->left, p, q);
        if (root->val < minVal)
            return lowestCommonAncestor(root->right, p, q);
        return root;
    }
};

701.二叉搜索树中的插入操作

题目链接
文章讲解

/**
 * 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* insertIntoBST(TreeNode* root, int val) {
        if (root == nullptr) return new TreeNode(val);
        TreeNode* cur = root;
        while (cur->left || cur->right) {
            if (cur->val > val)
                if (cur->left) cur = cur->left; else break;
            else
                if (cur->right) cur = cur->right; else break;
        }
        if (cur->val > val)
            cur->left = new TreeNode(val);
        else
            cur->right = new TreeNode(val);
        return root;
    }
};

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* deleteTragetNode(TreeNode* traget) {
        if (!traget->right) return traget->left;
        TreeNode* post = traget->right;
        while (post->left) post = post->left;
        post->left = traget->left;
        return traget->right;
    }

    TreeNode* deleteNode(TreeNode* root, int key) {
        TreeNode* parent = nullptr;
        TreeNode* keyNode = root;
        while (keyNode) {
            if (keyNode->val == key) break;
            parent = keyNode;
            if (keyNode->val > key)
                keyNode = keyNode->left;
            else 
                keyNode = keyNode->right;
        }
        if (!keyNode) return root;
        if (!parent) return deleteTragetNode(keyNode);
        if (parent->val > keyNode->val)
            parent->left = deleteTragetNode(keyNode);
        else
            parent->right = deleteTragetNode(keyNode);
        return root;
    }
};

你可能感兴趣的:(算法)