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/

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

友情链接更多精彩内容