提交 64b8451a 编写于 作者: L laozhang

python leetcode 653

上级 7d934694
......@@ -32,6 +32,7 @@
18. [对称二叉树](./solution/tree/leetcode_101_.py)
19. [二叉搜索树节点最小距离](./solution/tree/leetcode_783_.py)
20. [二叉树的坡度](./solution/tree/leetcode_563_.py)
21. [ 两数之和 IV - 输入 BST](./solution/tree/leetcode_653_.py)
......
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# coding=utf-8
"""
653. 两数之和 IV - 输入 BST
"""
from solution import TreeNode
class Solution:
def findTarget(self, root: TreeNode, k: int) -> bool:
res = []
def helper(root: TreeNode):
nonlocal res
if root:
helper(root.left)
res.append(root.val)
helper(root.right)
helper(root)
start = 0
end = len(res) - 1
while start < end:
if (res[start] + res[end]) > k:
end = end - 1
elif (res[start] + res[end]) < k:
start = start + 1
else:
return True
return False
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册