二叉树的结点删除要分为三类:
1、没有子结点
2、有一个子结点
3、有两个子结点
代码实现:
public void delete02(TreeNode node) {
TreeNode pre = null;
TreeNode temp = null;
if(node.right == null) {
node = node.left;
} else if(node.left == null) {
node = node.right;
} else {
temp = node.left;
pre = node;
while(temp.right != null) {
pre = temp;
temp = temp .right;
}
node.val = temp.val;
pre.right = temp.left;
}
temp = null;
}
也可以参考下面这个:
https://www.cnblogs.com/Michaelwjw/p/6384428.html