摘要: 一、题目 给出二叉 搜索 树的根节点,该树的节点值各不相同,请你将其转换为累加树(Greater Sum Tree),使每个节点 node 的新值等于原树中大于或等于 node.val 的值之和。 二、解法 反向中序遍历: class Solution { int sum=0; public Tre 阅读全文
posted @ 2022-02-16 15:54 livingsu 阅读(28) 评论(0) 推荐(0)
摘要: 递归: class Solution { public TreeNode trimBST(TreeNode root, int low, int high) { if(root==null) return root; if(root.val<low) return trimBST(root.righ 阅读全文
posted @ 2022-02-16 15:30 livingsu 阅读(23) 评论(0) 推荐(0)
摘要: 一、题目 给定一个二叉搜索树的根节点 root 和一个值 key,删除二叉搜索树中的 key 对应的节点,并保证二叉搜索树的性质不变。返回二叉搜索树(有可能被更新)的根节点的引用。 一般来说,删除节点可分为两个步骤: 首先找到需要删除的节点; 如果找到了,删除它。 二、解法 node表示找到的节点, 阅读全文
posted @ 2022-02-16 15:17 livingsu 阅读(20) 评论(0) 推荐(0)