|
| 1 | +/** |
| 2 | + * Definition for a binary tree node. |
| 3 | + * struct TreeNode { |
| 4 | + * int val; |
| 5 | + * TreeNode *left; |
| 6 | + * TreeNode *right; |
| 7 | + * TreeNode(int x) : val(x), left(NULL), right(NULL) {} |
| 8 | + * }; |
| 9 | + */ |
| 10 | +class Solution { |
| 11 | +public: |
| 12 | + TreeNode* get(TreeNode* root, int key){ |
| 13 | + if(!root){ |
| 14 | + return NULL; |
| 15 | + } |
| 16 | + if(root->val == key){ |
| 17 | + return root; |
| 18 | + } |
| 19 | + else if(root->val > key){ |
| 20 | + return get(root->left, key); |
| 21 | + } |
| 22 | + return get(root->right, key); |
| 23 | + } |
| 24 | + |
| 25 | + TreeNode* getSuc(TreeNode* root){ |
| 26 | + TreeNode* curr = root->right; |
| 27 | + |
| 28 | + while(curr->left){ |
| 29 | + curr = curr->left; |
| 30 | + } |
| 31 | + |
| 32 | + return curr; |
| 33 | + } |
| 34 | + |
| 35 | + TreeNode* getParent(TreeNode* root, TreeNode* key, bool& left){ |
| 36 | + if(!root){ |
| 37 | + return NULL; |
| 38 | + } |
| 39 | + else if(root->left == key){ |
| 40 | + left = true; |
| 41 | + return root; |
| 42 | + } |
| 43 | + else if(root->right == key){ |
| 44 | + return root; |
| 45 | + } |
| 46 | + else if(root->val < key->val){ |
| 47 | + return getParent(root->right, key, left); |
| 48 | + } |
| 49 | + return getParent(root->left, key, left); |
| 50 | + } |
| 51 | + |
| 52 | + TreeNode* deleteNode(TreeNode* root, int key) { |
| 53 | + TreeNode* node = get(root, key); |
| 54 | + |
| 55 | + if(!node){ |
| 56 | + return root; |
| 57 | + } |
| 58 | + |
| 59 | + bool left = false; |
| 60 | + TreeNode* parent = getParent(root, node, left); |
| 61 | + |
| 62 | + if(!node->left && !node->right){ |
| 63 | + if(parent){ |
| 64 | + if(left){ |
| 65 | + parent->left = NULL; |
| 66 | + } |
| 67 | + else{ |
| 68 | + parent->right = NULL; |
| 69 | + } |
| 70 | + } |
| 71 | + else{ |
| 72 | + return NULL; |
| 73 | + } |
| 74 | + } |
| 75 | + else if(!node->left){ |
| 76 | + if(parent){ |
| 77 | + if(left){ |
| 78 | + parent->left = node->right; |
| 79 | + } |
| 80 | + else{ |
| 81 | + parent->right = node->right; |
| 82 | + } |
| 83 | + } |
| 84 | + else{ |
| 85 | + return node->right; |
| 86 | + } |
| 87 | + } |
| 88 | + else if(!node->right){ |
| 89 | + if(parent){ |
| 90 | + if(left){ |
| 91 | + parent->left = node->left; |
| 92 | + } |
| 93 | + else{ |
| 94 | + parent->right = node->left; |
| 95 | + } |
| 96 | + } |
| 97 | + else{ |
| 98 | + return node->left; |
| 99 | + } |
| 100 | + } |
| 101 | + else{ |
| 102 | + TreeNode* successor = getSuc(node); |
| 103 | + successor->left = node->left; |
| 104 | + |
| 105 | + bool lft = false; |
| 106 | + TreeNode* succParent = getParent(root, successor, lft); |
| 107 | + |
| 108 | + if(succParent){ |
| 109 | + if(lft){ |
| 110 | + succParent->left = successor->right; |
| 111 | + } |
| 112 | + else{ |
| 113 | + succParent->right = successor->right; |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + successor->right = node->right; |
| 118 | + |
| 119 | + if(parent){ |
| 120 | + if(left){ |
| 121 | + parent->left = successor; |
| 122 | + } |
| 123 | + else{ |
| 124 | + parent->right = successor; |
| 125 | + } |
| 126 | + } |
| 127 | + else{ |
| 128 | + return successor; |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + return root; |
| 133 | + } |
| 134 | +}; |
0 commit comments