mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 03:34:02 +08:00
update 0669.修剪二叉搜索树: 优化代码风格
This commit is contained in:
@ -59,7 +59,7 @@ public:
|
|||||||

|

|
||||||
|
|
||||||
|
|
||||||
理解了最关键部分了我们在递归三部曲:
|
理解了最关键部分了我们再递归三部曲:
|
||||||
|
|
||||||
* 确定递归函数的参数以及返回值
|
* 确定递归函数的参数以及返回值
|
||||||
|
|
||||||
@ -179,7 +179,7 @@ public:
|
|||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
只看代码,其实不太好理解节点是符合移除的,这一块大家可以自己在模拟模拟!
|
只看代码,其实不太好理解节点是如何移除的,这一块大家可以自己再模拟模拟!
|
||||||
|
|
||||||
## 迭代法
|
## 迭代法
|
||||||
|
|
||||||
@ -301,19 +301,19 @@ class Solution:
|
|||||||
|
|
||||||
// 递归
|
// 递归
|
||||||
func trimBST(root *TreeNode, low int, high int) *TreeNode {
|
func trimBST(root *TreeNode, low int, high int) *TreeNode {
|
||||||
if root==nil{
|
if root == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
if root.Val<low{//如果该节点值小于最小值,则该节点更换为该节点的右节点值,继续遍历
|
if root.Val < low { //如果该节点值小于最小值,则该节点更换为该节点的右节点值,继续遍历
|
||||||
right:=trimBST(root.Right,low,high)
|
right := trimBST(root.Right, low, high)
|
||||||
return right
|
return right
|
||||||
}
|
}
|
||||||
if root.Val>high{//如果该节点的值大于最大值,则该节点更换为该节点的左节点值,继续遍历
|
if root.Val > high { //如果该节点的值大于最大值,则该节点更换为该节点的左节点值,继续遍历
|
||||||
left:=trimBST(root.Left,low,high)
|
left := trimBST(root.Left, low, high)
|
||||||
return left
|
return left
|
||||||
}
|
}
|
||||||
root.Left=trimBST(root.Left,low,high)
|
root.Left = trimBST(root.Left, low, high)
|
||||||
root.Right=trimBST(root.Right,low,high)
|
root.Right = trimBST(root.Right, low, high)
|
||||||
return root
|
return root
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -323,25 +323,25 @@ func trimBST(root *TreeNode, low int, high int) *TreeNode {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
// 处理 root,让 root 移动到[low, high] 范围内,注意是左闭右闭
|
// 处理 root,让 root 移动到[low, high] 范围内,注意是左闭右闭
|
||||||
for root != nil && (root.Val<low||root.Val>high){
|
for root != nil && (root.Val < low || root.Val > high) {
|
||||||
if root.Val < low{
|
if root.Val < low {
|
||||||
root = root.Right
|
root = root.Right
|
||||||
}else{
|
} else {
|
||||||
root = root.Left
|
root = root.Left
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
cur := root
|
cur := root
|
||||||
// 此时 root 已经在[low, high] 范围内,处理左孩子元素小于 low 的情况(左节点是一定小于 root.Val,因此天然小于 high)
|
// 此时 root 已经在[low, high] 范围内,处理左孩子元素小于 low 的情况(左节点是一定小于 root.Val,因此天然小于 high)
|
||||||
for cur != nil{
|
for cur != nil {
|
||||||
for cur.Left!=nil && cur.Left.Val < low{
|
for cur.Left != nil && cur.Left.Val < low {
|
||||||
cur.Left = cur.Left.Right
|
cur.Left = cur.Left.Right
|
||||||
}
|
}
|
||||||
cur = cur.Left
|
cur = cur.Left
|
||||||
}
|
}
|
||||||
cur = root
|
cur = root
|
||||||
// 此时 root 已经在[low, high] 范围内,处理右孩子大于 high 的情况
|
// 此时 root 已经在[low, high] 范围内,处理右孩子大于 high 的情况
|
||||||
for cur != nil{
|
for cur != nil {
|
||||||
for cur.Right!=nil && cur.Right.Val > high{
|
for cur.Right != nil && cur.Right.Val > high {
|
||||||
cur.Right = cur.Right.Left
|
cur.Right = cur.Right.Left
|
||||||
}
|
}
|
||||||
cur = cur.Right
|
cur = cur.Right
|
||||||
@ -359,24 +359,24 @@ var trimBST = function(root, low, high) {
|
|||||||
if(root === null) {
|
if(root === null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
while(root !==null &&(root.val<low||root.val>high)) {
|
while(root !== null && (root.val < low || root.val > high)) {
|
||||||
if(root.val<low) {
|
if(root.val < low) {
|
||||||
root = root.right;
|
root = root.right;
|
||||||
}else {
|
}else {
|
||||||
root = root.left;
|
root = root.left;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let cur = root;
|
let cur = root;
|
||||||
while(cur!==null) {
|
while(cur !== null) {
|
||||||
while(cur.left&&cur.left.val<low) {
|
while(cur.left && cur.left.val < low) {
|
||||||
cur.left = cur.left.right;
|
cur.left = cur.left.right;
|
||||||
}
|
}
|
||||||
cur = cur.left;
|
cur = cur.left;
|
||||||
}
|
}
|
||||||
cur = root;
|
cur = root;
|
||||||
//判断右子树大于high的情况
|
//判断右子树大于high的情况
|
||||||
while(cur!==null) {
|
while(cur !== null) {
|
||||||
while(cur.right&&cur.right.val>high) {
|
while(cur.right && cur.right.val > high) {
|
||||||
cur.right = cur.right.left;
|
cur.right = cur.right.left;
|
||||||
}
|
}
|
||||||
cur = cur.right;
|
cur = cur.right;
|
||||||
@ -391,16 +391,16 @@ var trimBST = function (root,low,high) {
|
|||||||
if(root === null) {
|
if(root === null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
if(root.val<low) {
|
if(root.val < low) {
|
||||||
let right = trimBST(root.right,low,high);
|
let right = trimBST(root.right, low, high);
|
||||||
return right;
|
return right;
|
||||||
}
|
}
|
||||||
if(root.val>high) {
|
if(root.val > high) {
|
||||||
let left = trimBST(root.left,low,high);
|
let left = trimBST(root.left, low, high);
|
||||||
return left;
|
return left;
|
||||||
}
|
}
|
||||||
root.left = trimBST(root.left,low,high);
|
root.left = trimBST(root.left, low, high);
|
||||||
root.right = trimBST(root.right,low,high);
|
root.right = trimBST(root.right, low, high);
|
||||||
return root;
|
return root;
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
Reference in New Issue
Block a user