450 Delete Node in a BST 删除二叉搜索树中的结点
详见:https://leetcode.com/problems/delete-node-in-a-bst/description/
C++:
/** * 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* deleteNode(TreeNode* root, int key) { if (!root) { return nullptr; } if (root->val > key) { root->left = deleteNode(root->left, key); } else if (root->val < key) { root->right = deleteNode(root->right, key); } else { if (!root->left || !root->right) { root = (root->left) ? root->left : root->right; } else { TreeNode *cur = root->right; while (cur->left) { cur = cur->left; } root->val = cur->val; root->right = deleteNode(root->right, cur->val); } } return root; } };
参考:https://www.cnblogs.com/grandyang/p/6228252.html