LC 450. Delete Node in a BST
Given a root node reference of a BST and a key, delete the node with the given key in the BST. Return the root node reference (possibly updated) of the BST.
Basically, the deletion can be divided into two stages:
- Search for a node to remove.
- If the node is found, delete the node.
Note: Time complexity should be O(height of tree).
Example:
- root = [5,3,6,2,4,null,7]
- key = 3
- 5
- / \
- 3 6
- / \ \
- 2 4 7
- Given key to delete is 3. So we find the node with value 3 and delete it.
- One valid answer is [5,4,6,2,null,null,7], shown in the following BST.
- 5
- / \
- 4 6
- / \
- 2 7
- Another valid answer is [5,2,6,null,4,null,7].
- 5
- / \
- 2 6
- \ \
- 4 7
- class Solution {
- public:
- TreeNode* deleteNode(TreeNode* root, int key) {
- if(!root) return root;
- TreeNode* ret;
- if(root->val == key) {
- TreeNode* rnode_lmost = getlm_or_rm_node(root->right, true);
- if(rnode_lmost) {
- rnode_lmost->left = root->left;
- ret = root->right;
- }else ret = root->left;
- }else {
- if(key < root->val) root->left = deleteNode(root->left, key);
- else root->right = deleteNode(root->right, key);
- ret = root;
- }
- return ret;
- }
- TreeNode* getlm_or_rm_node(TreeNode* root, bool left){
- if(!root) return root;
- if(left) {
- while(root->left) root = root->left;
- }else {
- while(root->right) root = root->right;
- }
- return root;
- }
- };
- class Solution {
- public:
- TreeNode *deleteNode(TreeNode *root, int key) {
- TreeNode **cur = &root;
- while (*cur && (*cur)->val != key)
- cur = (key > (*cur)->val) ? &(*cur)->right : &(*cur)->left;
- if (*cur) {
- if (!(*cur)->right) *cur = (*cur)->left;
- else {
- TreeNode **successor = &(*cur)->right;
- while ((*successor)->left) successor = &(*successor)->left;
- swap((*cur)->val, (*successor)->val);
- *successor = (*successor)->right ? (*successor)->right : nullptr;
- }
- }
- return root;
- }
- };
LC 450. Delete Node in a BST的更多相关文章
- [LeetCode] 450. Delete Node in a BST 删除二叉搜索树中的节点
Given a root node reference of a BST and a key, delete the node with the given key in the BST. Retur ...
- 450. Delete Node in a BST 删除bst中的一个节点
[抄题]: Given a root node reference of a BST and a key, delete the node with the given key in the BST. ...
- [Leetcode]450. Delete Node in a BST
Given a root node reference of a BST and a key, delete the node with the given key in the BST. Retur ...
- LeetCode OJ 450. Delete Node in a BST
Given a root node reference of a BST and a key, delete the node with the given key in the BST. Retur ...
- 450. Delete Node in a BST
Given a root node reference of a BST and a key, delete the node with the given key in the BST. Retur ...
- 【leetcode】 450. Delete Node in a BST
Given a root node reference of a BST and a key, delete the node with the given key in the BST. Retur ...
- 【LeetCode】450. Delete Node in a BST 解题报告 (Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 迭代 日期 题目地址:https://leetcode ...
- 450 Delete Node in a BST 删除二叉搜索树中的结点
详见:https://leetcode.com/problems/delete-node-in-a-bst/description/ C++: /** * Definition for a binar ...
- [leetcode]450. Delete Node in a BST二叉搜索树删除节点
二叉树变量只是一个地址 public static void main(String[] args) { TreeNode t = new TreeNode(3); help(t); System.o ...
随机推荐
- Windows下动态库的制作与使用
创建静态库
- charles 主界面总结
本文参考:charles 主界面总结 charles 主界面的介绍 Charles 主要提供两种查看封包的视图,分别名为 Structure Structure/结构视图,将网络请求按访问的域名分类, ...
- Luogu P1892 团伙
Luogu P1892 团伙 这是道很简单的并查集-- 不,它并不简单. 这道题考了一个叫做反集的东西. 也就是说: 如果$a$和$b$是敌人,合并$n+b$和$a$,$n+a$和$b$: 如果$c$ ...
- 详细讲解vue.js里的父子组件通信(props和$emit)
在进入这个话题之前,首先我们先来想一下在vue里,如何写一个父子组件.为了简单起见,下面的代码我都没用脚手架来构建项目,直接在html文件里引入vue.js来作为例子.父子组件的写法如下: <d ...
- vueRouter history模式下 nginx配置
对于VUE的router[mode: history]模式(这里主要是为了去除链接上的"#")开发的时候,一般都不出问题.是因为开发时用的服务器为node,Dev环境中已配置好了, ...
- DML子句returing into用法举例
一.概述: ORACLE的DML语句中可以指定RETURNING语句.使用起来也很简单,和SELECT INTO语句没有多大区别.RETURNING语句的使用在很多情况下可以简化PL/SQL编程. I ...
- Scrapy框架的八个扩展
一.proxies代理 首先需要在环境变量中设置 from scrapy.contrib.downloadermiddleware.httpproxy import HttpProxyMiddlewa ...
- LightOJ-1020-A Childhood Game(博弈)
链接: https://vjudge.net/problem/LightOJ-1020 题意: Alice and Bob are playing a game with marbles; you m ...
- docker-compose.yml的使用
docker-compose.yml包含version.services.networks3大部分 services的书写规则 1.iamge services: web: # 服务名称,用户自定义 ...
- java实现大视频上传
javaweb上传文件 上传文件的jsp中的部分 上传文件同样可以使用form表单向后端发请求,也可以使用 ajax向后端发请求 1.通过form表单向后端发送请求 <form id=" ...