From 10302b0a143d0b0c736466da197c6dba8ff76c07 Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Mon, 5 Dec 2022 14:33:48 +0800 Subject: [PATCH] =?UTF-8?q?update=200669.=E4=BF=AE=E5=89=AA=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91:=20=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=E9=A3=8E=E6=A0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0669.修剪二叉搜索树.md | 56 +++++++++++++------------- 1 file changed, 28 insertions(+), 28 deletions(-) diff --git a/problems/0669.修剪二叉搜索树.md b/problems/0669.修剪二叉搜索树.md index 98af3c26..c2d7b79b 100644 --- a/problems/0669.修剪二叉搜索树.md +++ b/problems/0669.修剪二叉搜索树.md @@ -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.Valhigh{//如果该节点的值大于最大值,则该节点更换为该节点的左节点值,继续遍历 - 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.Valhigh){ - 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.valhigh)) { - if(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.valhigh) { + 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.valhigh) { - 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; } ```