# leetcode 653 两数之和

LukeRivel · · 208 次点击 · · 开始浏览

# self.right = None

class Solution(object):

``````def findTarget(self, root, k):
"""
:type root: TreeNode
:type k: int
:rtype: bool
"""
if not root or (not root.left and not root.right):
return False

self.values = []
self.inOrder(root)
for i, v in enumerate(self.values):
if self.values[i] != k - v and (k - v) in self.values:
return True
return False

def inOrder(self, node):
if node:
self.inOrder(node.left)
self.values.append(node.val)
self.inOrder(node.right)
``````

0 回复

• 请尽量让自己的回复能够对别人有帮助
• 支持 Markdown 格式, **粗体**、~~删除线~~、``单行代码``
• 支持 @ 本站用户；支持表情（输入 : 提示），见 Emoji cheat sheet
• 图片支持拖拽、截图粘贴等方式上传

# self.right = None

class Solution(object):

``````def findTarget(self, root, k):
"""
:type root: TreeNode
:type k: int
:rtype: bool
"""
if not root or (not root.left and not root.right):
return False

self.values = []
self.inOrder(root)
for i, v in enumerate(self.values):
if self.values[i] != k - v and (k - v) in self.values:
return True
return False

def inOrder(self, node):
if node:
self.inOrder(node.left)
self.values.append(node.val)
self.inOrder(node.right)
``````