diff --git "a/problems/0300.346円234円200円351円225円277円344円270円212円345円215円207円345円255円220円345円272円217円345円210円227円.md" "b/problems/0300.346円234円200円351円225円277円344円270円212円345円215円207円345円255円220円345円272円217円345円210円227円.md" index e8cb0b5f18..01d349496b 100644 --- "a/problems/0300.346円234円200円351円225円277円344円270円212円345円215円207円345円255円220円345円272円217円345円210円227円.md" +++ "b/problems/0300.346円234円200円351円225円277円344円270円212円345円215円207円345円255円220円345円272円217円345円210円227円.md" @@ -149,7 +149,7 @@ class Solution: if len(nums) <= 1: return len(nums) dp = [1] * len(nums) - result = 0 + result = 1 for i in range(1, len(nums)): for j in range(0, i): if nums[i]> nums[j]: diff --git "a/problems/0669.344円277円256円345円211円252円344円272円214円345円217円211円346円220円234円347円264円242円346円240円221円.md" "b/problems/0669.344円277円256円345円211円252円344円272円214円345円217円211円346円220円234円347円264円242円346円240円221円.md" index 95372d6196..85922a1dd8 100644 --- "a/problems/0669.344円277円256円345円211円252円344円272円214円345円217円211円346円220円234円347円264円242円346円240円221円.md" +++ "b/problems/0669.344円277円256円345円211円252円344円272円214円345円217円211円346円220円234円347円264円242円346円240円221円.md" @@ -248,6 +248,8 @@ public: ## Java +**递归** + ```Java class Solution { public TreeNode trimBST(TreeNode root, int low, int high) { @@ -269,6 +271,46 @@ class Solution { ``` +**迭代** + +```Java +class Solution { + //iteration + public TreeNode trimBST(TreeNode root, int low, int high) { + if(root == null) + return null; + while(root != null && (root.val < low || root.val> high)){ + if(root.val < low) + root = root.right; + else + root = root.left; + } + + TreeNode curr = root; + + //deal with root's left sub-tree, and deal with the value smaller than low. + while(curr != null){ + while(curr.left != null && curr.left.val < low){ + curr.left = curr.left.right; + } + curr = curr.left; + } + //go back to root; + curr = root; + + //deal with root's righg sub-tree, and deal with the value bigger than high. + while(curr != null){ + while(curr.right != null && curr.right.val> high){ + curr.right = curr.right.left; + } + curr = curr.right; + } + return root; + } +} + +```` + ## Python 递归法(版本一)