Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions Codes/0669-trim-a-binary-search-tree.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* @Author: laughWinter
* @Date: 2025-10-26 23:16:50
* @LastEditors: LetMeFly.xyz
* @LastEditTime: 2025-10-27 09:42:30
*/
// import javax.swing.tree.TreeNode;

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/

class Solution {
public TreeNode trimBST(TreeNode root, int low, int high) {
//寻找新的根节点
while ((root != null) && ((root.val < low) || (root.val > high))) {
if (root.val < low) {
root = root.right;
} else if (root.val > high) {
root = root.left;
}
}
if (root == null) {
return null;
}
//修建root的左右子树
TreeNode tmp = root;
while (tmp.left != null) {
if (tmp.left.val < low) {
tmp.left = tmp.left.right;
} else {
tmp = tmp.left;
}
}
tmp = root;
while (tmp.right != null) {
if (tmp.right.val > high) {
tmp.right = tmp.right.left;
} else {
tmp = tmp.right;
}
}

return root;
}
}
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @Author: LetMeFly
* @Date: 2022-05-19 18:48:53
* @LastEditors: LetMeFly.xyz
* @LastEditTime: 2025-09-27 13:24:44
* @LastEditTime: 2025-10-27 10:03:05
-->
# LetLeet Blog

Expand Down Expand Up @@ -33,6 +33,12 @@

在线博客:[blog.letmefly.xyz](https://blog.letmefly.xyz/)

真诚感谢本仓库的贡献者!

|贡献者|贡献内容|
|:--:|:--:|
|[laughWinter](https://github.com/laughWinter)|添加LeetCode669.Java版本的代码[#1186](https://github.com/LetMeFly666/LeetCode/pull/1186)|

## 技术思考

|名称|博客|CSDN博客地址|
Expand Down