Lintcode87 Remove Node In Binary Search Treesolution 题解

【题目描述】

Given a root of Binary Search Tree with unique value for each node. Remove the node with given value. If there is no such a node with given value in the binary search tree, do nothing. You should keep the tree still a binary search tree after removal.

给定一棵具有不同节点值的二叉查找树,删除树中与给定值相同的节点。如果树中没有相同值的节点,就不做任何处理。你应该保证处理之后的树仍是二叉查找树。

【题目链接】

www.lintcode.com/en/problem/remove-node-in-binary-search-tree/

【题目解析】

首先根据BST(二叉查找树)的性质,找到目标节点cur及其父节点pre

如果cur不存在,则直接返回root。记ncur为取代cur位置的节点,默认令ncur = cur.right。如果cur拥有左孩子,则将其右孩子链接到左孩子的最大子节点的右侧,令cur = cur.left。然后修正pre与ncur之间的关系。

【参考答案】

www.jiuzhang.com/solutions/remove-node-in-binary-search-tree/


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值