653. 两数之和 IV - 输入 BST

题目链接:https://leetcode-cn.com/problems/two-sum-iv-input-is-a-bst/

执行用时:136 ms, 在所有 Swift 提交中击败了81.48% 的用户
内存消耗:15.8 MB, 在所有 Swift 提交中击败了29.63% 的用户

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class Solution {
func findTarget(_ root: TreeNode?, _ k: Int) -> Bool {
var nums = [Int]()
func inOrder(_ root: TreeNode?) {
if root == nil {
return
}
inOrder(root?.left)
nums.append(root!.val)
inOrder(root?.right)
}
inOrder(root)
var l = 0, r = nums.count - 1, sum = 0
while l < r {
sum = nums[l] + nums[r]
if sum == k {
return true
}
if sum > k {
r -= 1
}else {
l += 1
}
}
return false
}
}

653. 两数之和 IV - 输入 BST
https://pisces34.github.io/2021/10/02/leetcode/653/
发布于
2021年10月2日
许可协议