update 0669.修剪二叉搜索树: 优化代码风格

This commit is contained in:
Yuhao Ju
2022-12-05 14:33:48 +08:00
committed by GitHub
parent 2835ac55c7
commit 10302b0a14

View File

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