二叉树变量只是一个地址

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二叉搜索树删除节点的更多相关文章

  1. [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 ...

  2. [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 ...

  3. Leetcode 450.删除二叉搜索树的节点

    删除二叉搜索树的节点 给定一个二叉搜索树的根节点 root 和一个值 key,删除二叉搜索树中的 key 对应的节点,并保证二叉搜索树的性质不变.返回二叉搜索树(有可能被更新)的根节点的引用. 一般来 ...

  4. LeetCode初级算法--树02:验证二叉搜索树

    LeetCode初级算法--树02:验证二叉搜索树 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:https://blog.csdn.ne ...

  5. 数据结构中很常见的各种树(BST二叉搜索树、AVL平衡二叉树、RBT红黑树、B-树、B+树、B*树)

    数据结构中常见的树(BST二叉搜索树.AVL平衡二叉树.RBT红黑树.B-树.B+树.B*树) 二叉排序树.平衡树.红黑树 红黑树----第四篇:一步一图一代码,一定要让你真正彻底明白红黑树 --- ...

  6. 二叉搜索树的结构(30 分) PTA 模拟+字符串处理 二叉搜索树的节点插入和非递归遍历

    二叉搜索树的结构(30 分) PTA 模拟+字符串处理 二叉搜索树的节点插入和非递归遍历   二叉搜索树的结构(30 分) 二叉搜索树或者是一棵空树,或者是具有下列性质的二叉树: 若它的左子树不空,则 ...

  7. [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 ...

  8. [LeetCode] Serialize and Deserialize BST 二叉搜索树的序列化和去序列化

    Serialization is the process of converting a data structure or object into a sequence of bits so tha ...

  9. [LeetCode] Minimum Absolute Difference in BST 二叉搜索树的最小绝对差

    Given a binary search tree with non-negative values, find the minimum absolute difference between va ...

随机推荐

  1. 在django中使用原生sql语句

    raw # row方法:(掺杂着原生sql和orm来执行的操作) res = CookBook.objects.raw('select id as nid from epos_cookbook whe ...

  2. java ipv6发邮件需要注意的点

    和ipv4发邮件一样,毕竟ip只是用来找地址的,v4 v6使用上基本没区别. 但有一点得注意:java ipv6采用发送RST包来通知邮件服务器断开连接,这样会导致客户端抛 MessagingExce ...

  3. RedHat-Linux操作指令第1篇

    不同的linux系统切换方式会稍有一点差别 从图形界面切换到字符界面:Alt+F(1-8) 或者 Alt+Ctrl+Shift+F(1-8) 从字符界面切换回图形界面:Alt+F7 字符界面启动到图形 ...

  4. c++如何按照map的value进行排序?

    static bool cmp(pair<char, int> a , pair<char,int> b) { return a.second>b.second; //按 ...

  5. linux服务器性能分析只需1分钟

    背景: 现在的互联网公司,大多数时候应用服务都是部署在linux服务器上,那么当你的服务运行过程中出现了一些响应慢,资源瓶颈等疑似性能问题时,给你60秒,如何快速完成初步检测? 肯定有人会说用工具,公 ...

  6. 题解-CF436E Cardboard Box

    题面 CF436E Cardboard Box \(n\) 个关卡,对每个关卡可以花 \(a_i\) 时间得到 \(1\) 颗星,或花 \(b_i\) 时间得到 \(2\) 颗星,或不玩.问获得 \( ...

  7. AcWing 398. 交通实时查询系统

    大型补档计划 题目链接 只有割点是必行点. 在任意一个点双中,都有分叉没有点交集的两条路径. 所以 v-DCC 缩点. 但是他问的是路径走到另一条路径的必行点.我蒙蔽了,发现自己对无向图双联通分量理解 ...

  8. Codeforces Edu Round 47 A-E

    A. Game Shopping 按照题意模拟既可. #include <iostream> #include <cstdio> using namespace std; co ...

  9. elasticsearch的基本了解

     以下内容参考官方文档https://www.elastic.co/guide/en/elasticsearch/reference/7.2/elasticsearch-intro.html 使用的学 ...

  10. AWT05-对话框

    1.Dialog Dialog组件是Window的子类,是容器类,是特殊组件. Dialog是可以独立存在的顶级窗口,使用上和普通窗口几乎没有区别,但应注意以下两点: 1.对话框通常依赖于其他窗口,也 ...