[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 ...
随机推荐
- C中memcpy函数用法
1.函数原型 void *memcpy(void *destin,void *source,unsigned n); 其中, destin代表用于存储复制内容的目标数组,类型强制转换为void*指针. ...
- Beta冲刺随笔——Day_Two
这个作业属于哪个课程 软件工程 (福州大学至诚学院 - 计算机工程系) 这个作业要求在哪里 Beta 冲刺 这个作业的目标 团队进行Beta冲刺 作业正文 正文 其他参考文献 无 今日事今日毕 林涛: ...
- 单体->集群->模块化->分布式微服务
开头语: 每篇一段开头语,在技术的道路中寻找文采的乐趣.(如果随笔中都是大白话勿喷,兄弟姐妹们) 单体项目 单体项目适用于小型开发,或自己来进行小项目的测试和使用. 单体项目的缺憾 多人开发项目所出现 ...
- 在django中使用原生sql语句
raw # row方法:(掺杂着原生sql和orm来执行的操作) res = CookBook.objects.raw('select id as nid from epos_cookbook whe ...
- PyQt(Python+Qt)学习随笔:QTreeWidgetItem项下子项的指示符展示原则childIndicatorPolicy
老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 树型部件QTreeWidget中的QTreeWidgetItem项下可以有子项,如果存在子项,则父项 ...
- PyQt(Python+Qt)学习随笔:QAbstractItemView的showDropIndicator属性
老猿Python博文目录 老猿Python博客地址 概述 QAbstractItemView的showDropIndicator属性用于控制在拖拽过程中显示当前拖拽到的位置,当释放时则在当前拖拽位置覆 ...
- DFS,BFS 练习(深搜,广搜,图,leetcode)
https://leetcode-cn.com/problems/route-between-nodes-lcci/ 节点间通路.给定有向图,设计一个算法,找出两个节点之间是否存在一条路径. 示例1: ...
- 题解-JSOI2011 分特产
题面 JSOI2011 分特产 有 \(n\) 个不同的盒子和 \(m\) 种不同的球,第 \(i\) 种球有 \(a_i\) 个,用光所有球,求使每个盒子不空的方案数. 数据范围:\(1\le n, ...
- 题解-SHOI2005 树的双中心
SHOI2005 树的双中心 给树 \(T=(V,E)(|V|=n)\),树高为 \(h\),\(w_u(u\in V)\).求 \(x\in V,y\in V:\left(\sum_{u\in V} ...
- Angular:组件之间的通信@Input、@Output和ViewChild
①父组件给子组件传值 1.父组件: ts: export class HomeComponent implements OnInit { public hxTitle = '我是首页的头部'; con ...