[leetcode]450. Delete Node in a BST二叉搜索树删除节点
二叉树变量只是一个地址
public static void main(String[] args) {
TreeNode t = new TreeNode(3);
help(t);
System.out.println(t.val);
}
public static void help(TreeNode t)
{
t.val = 6;
}
上边代码通过地址改变了二叉树,输出为6,但是下边代码却只是传入函数的二叉树变量指向了另一个地址,外界的二叉树变量和二叉树的值没有变,输出还是3
public static void main(String[] args) {
TreeNode t = new TreeNode(3);
help(t);
System.out.println(t.val);
}
public static void help(TreeNode t)
{
t = new TreeNode6);
}
所以想改变二叉树,不能改变二叉树变量,而应该通过二叉树变量t调用val,left,right进行赋值,就可以改变,直接改变t只是让t指向另一课树,原本的树没有改变。
下边是答案,思路是先找到节点,再根据节点的不同情况进行操作。
最后的操作很乱,自己都看不下去了,应该递归的改变左右子树,但是眼睛太累了,有空再改吧。
public TreeNode deleteNode(TreeNode root, int key) {
if(root==null) return null;
if(root.val==key)
{
if (root.left==null) return root.right;
if (root.right==null) return root.left;
TreeNode temp = root.right;
while (temp.left!=null) temp = temp.left;
if (root.left.right!=null) temp.left = root.left.right;
root.left.right = root.right;
root.val = root.left.val;
if (root.left.left==null)
{
root.right = root.left.right;
root.left = null;
}
else {
//这里注意,由于两句话都要用到root.left,所以root.left最后再变
root.right = root.left.right;
root.left = root.left.left;
}
}
else
{
if(root.val>key) root.left = deleteNode(root.left,key);
else root.right = deleteNode(root.right,key);
}
return root;
}
[leetcode]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 ...
- [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.删除二叉搜索树的节点
删除二叉搜索树的节点 给定一个二叉搜索树的根节点 root 和一个值 key,删除二叉搜索树中的 key 对应的节点,并保证二叉搜索树的性质不变.返回二叉搜索树(有可能被更新)的根节点的引用. 一般来 ...
- LeetCode初级算法--树02:验证二叉搜索树
LeetCode初级算法--树02:验证二叉搜索树 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:https://blog.csdn.ne ...
- 数据结构中很常见的各种树(BST二叉搜索树、AVL平衡二叉树、RBT红黑树、B-树、B+树、B*树)
数据结构中常见的树(BST二叉搜索树.AVL平衡二叉树.RBT红黑树.B-树.B+树.B*树) 二叉排序树.平衡树.红黑树 红黑树----第四篇:一步一图一代码,一定要让你真正彻底明白红黑树 --- ...
- 二叉搜索树的结构(30 分) PTA 模拟+字符串处理 二叉搜索树的节点插入和非递归遍历
二叉搜索树的结构(30 分) PTA 模拟+字符串处理 二叉搜索树的节点插入和非递归遍历 二叉搜索树的结构(30 分) 二叉搜索树或者是一棵空树,或者是具有下列性质的二叉树: 若它的左子树不空,则 ...
- [LeetCode] Kth Smallest Element in a BST 二叉搜索树中的第K小的元素
Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. Not ...
- [LeetCode] Serialize and Deserialize BST 二叉搜索树的序列化和去序列化
Serialization is the process of converting a data structure or object into a sequence of bits so tha ...
- [LeetCode] Minimum Absolute Difference in BST 二叉搜索树的最小绝对差
Given a binary search tree with non-negative values, find the minimum absolute difference between va ...
随机推荐
- JZOJ2020年10月5日提高B组反思
2020年10月5日提高B组反思 T1 考试的时候想简单了 觉得把跟没有攻占的点相连的边留下就可以了 没有考虑到最小 WA&RE 10 T2 没有思路 就直接从中间往后枚举分解处 蜜汁错误 W ...
- 浅尝 Elastic Stack (三) Logstash + Beats
本文使用 Filebeat,如果没有安装需要安装: curl -L -O https://artifacts.elastic.co/downloads/beats/filebeat/filebeat- ...
- vue-父子组件传参以及无限级评论
vue父子组件的使用 <template> <div> <zi :data="data" /> </div> </templa ...
- 并发编程实战-J.U.C核心包
J.U.C - AQS java.util.concurrent(J.U.C)大大提高了并发性能,AQS 被认为是 J.U.C 的核心.它核心是利用volatile和一个维护队列. AQS其实就是ja ...
- 第二十五章、containers容器类部件GroupBox分组框详解
老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 一.概述 容器部件就是可以在部件内放置其他部件的部件,在Qt Designer中可以使用的容器部件有 ...
- [BJDCTF 2nd]duangShell 反弹shell
[BJDCTF 2nd]duangShell [BJDCTF 2nd]duangShell 点击进去之后提示我们swp源代码泄露,访问http://xxx/.index.php.swp下载该文件 ...
- 从输入 URL 到页面展示,这中间发生了什么?
当面试官问到,请你说说看"从输入 URL 到页面展示,这中间发生了什么?" 以前的我是这样回答的: 用户输入URL后,向服务器端发起请求.如果顺利,得到网络响应之后,浏览器对资源进 ...
- 转:解释lsh
Locality sensitive hashing - LSH explained The problem of finding duplicate documents in a list may ...
- 【APIO2019】路灯(ODT & (树套树 | CDQ分治))
Description 一条 \(n\) 条边,\(n+1\) 个点的链,边有黑有白.若结点 \(a\) 可以到达 \(b\),需要满足 \(a\to b\) 的路径上的边不能有黑的.现给出 \(0\ ...
- Linux相关介绍和安装
目录 前言 第一章 linux介绍 1.1 linux简介 第二章 Linux起源 2.1 Unix的历史 2.2 Unix操作系统的革命 2.3 Linux系统的诞生 2.4 Linux系统的发展史 ...