注册一亩三分地论坛,查看更多干货!
您需要 登录 才可以下载或查看附件。没有帐号?注册账号 
x
http://www.lintcode.com/en/probl ... binary-search-tree/感觉按照这个思路非常好理解
http://collabedit.com/abtum
public TreeNode removeNode(TreeNode root, int value) {
if (root == null) {
return null;
}
// If the key to be deleted is smaller than the root's key, then it lies
// in left subtree
if (value < root.val) {
root.left = removeNode(root.left, value);
}
if (value > root.val) {
root.right = removeNode(root.right, value);
}
// if key is same as root's key, then This is the node to be deleted
if (value == root.val) {
if (root.right == null) {
return root.left;
}
if (root.left == null) {
return root.right;
}// Get the in-order successor (smallest in the right subtree) and
// delete it from the subtree
TreeNode successor = root.right;
while (successor.left != null) {
successor = successor.left;
} // Copy the in-order successor's content to this node
root.val = successor.val; // Delete the in-order successor
root.right = removeNode(root.right, successor.val);
}
return root;
}
|