**Leetcode 450. Delete Node in a BST

Posted Z-Pilgrim

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了**Leetcode 450. Delete Node in a BST相关的知识,希望对你有一定的参考价值。

https://leetcode.com/problems/delete-node-in-a-bst/description/

不错的题 有一些细节需要注意

 

/**
 * 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* dfs(TreeNode *root, int k, int &get) 
        if (!root || get) return root;
        if (k == root->val) 
            get = 1;
            TreeNode * right = root->left, *pre = NULL;
            if (!right) return root->right;
            while (right->right) pre = right, right = right -> right;
            if (right == root->left) 
                right ->right = root->right;
                return right;
             else 
                pre->right = right->left;
                right->left = root->left;
                right->right = root->right;
                return right;
            
        
        
        root->left = dfs(root->left, k, get);
        root->right = dfs(root->right, k, get);
        return root;
    
    
    TreeNode* deleteNode(TreeNode* root, int key) 
        int get = 0;
        return dfs(root, key, get);
    
;

 

以上是关于**Leetcode 450. Delete Node in a BST的主要内容,如果未能解决你的问题,请参考以下文章

[LeetCode] 450. Delete Node in a BST

[leetcode]450. Delete Node in a BST二叉搜索树删除节点

450. Delete Node in a BST

450. Delete Node in a BST

450. Delete Node in a BST

450. Delete Node in a BST