当前位置:网站首页>小黑重新站起来看leetcode:653. 两数之和 IV - 输入 BST
小黑重新站起来看leetcode:653. 两数之和 IV - 输入 BST
2022-07-28 09:43:00 【小黑无敌】
小黑答案
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def findTarget(self, root: Optional[TreeNode], k: int) -> bool:
if not root:
return False
arr = []
def pre_tree(node):
if node:
arr.append(node.val)
pre_tree(node.left)
pre_tree(node.right)
pre_tree(root)
for t,i in enumerate(arr):
if k - i in arr[:t] or k - i in arr[t+1:]:
return True
return False

深度优先
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def findTarget(self, root: Optional[TreeNode], k: int) -> bool:
route = set()
def deep_search(node):
if not node:
return False
if k - node.val in route:
return True
route.add(node.val)
return deep_search(node.left) or deep_search(node.right)
return deep_search(root)

宽度优先
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def findTarget(self, root: Optional[TreeNode], k: int) -> bool:
if not root:
return False
route = set()
q = deque([root])
while q:
node = q.popleft()
if k - node.val in route:
return True
route.add(node.val)
if node.left:
q.append(node.left)
if node.right:
q.append(node.right)
return False

深度优先搜索 + 中序遍历 + 双指针
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def findTarget(self, root: Optional[TreeNode], k: int) -> bool:
if not root:
return False
arr = []
def mid_display(node):
if node:
mid_display(node.left)
arr.append(node.val)
mid_display(node.right)
mid_display(root)
s,e = 0,len(arr) - 1
while s != e:
if arr[s]+arr[e] > k:
e -= 1
elif arr[s]+arr[e] == k:
return True
else:
s += 1
return False

迭代+中序遍历
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def findTarget(self, root: Optional[TreeNode], k: int) -> bool:
if not root:
return False
left_nodes = [root]
right_nodes = [root]
left,right = root,root
while left.left:
left = left.left
left_nodes.append(left)
while right.right:
right = right.right
right_nodes.append(right)
while right != left:
sum_ = right.val+left.val
if sum_ == k:
return True
elif sum_ < k:
left = left_nodes.pop()
node = left.right
while node:
left_nodes.append(node)
node = node.left
else:
right = right_nodes.pop()
node = right.left
while node:
right_nodes.append(node)
node = node.right
return False

边栏推荐
- NTU Lin Xuantian's "machine learning cornerstone" problem solving and code implementation | [you deserve it]
- OSPF的LSA及优化
- PHP 获取接口的方式
- Read Plato farm's eplato and the reason for its high premium
- Openatom openharmony sub forum, see you today at 14:00! Wonderful release of memorabilia attached
- SQL Server、MySQL主从搭建,EF Core读写分离代码实现
- Espresso systems, which has just obtained financing, has both intellectual property rights and team ethics in trouble
- Shell implements the backup / recovery / migration functions of harbor v1/v2
- Plato farm - a farm meta universe game with Plato as the goal
- 备受关注的Bit.Store,最新动态一览
猜你喜欢

这种动态规划你见过吗——状态机动态规划之股票问题(中)
![[OpenHarmony] [RK2206] 构建OpenHarmony编译器 (二)](/img/0c/2e8290403d64ec43d192969f776724.png)
[OpenHarmony] [RK2206] 构建OpenHarmony编译器 (二)

每天在岗不足8小时被辞?腾讯前员工追讨1300万加班费等,法院终审获赔9万

3分钟告诉你如何成为一名黑客|零基础到黑客入门指南,你只需要掌握这五点能力

医药行业数字化建设,箭在弦上

pkg打包node工程

The blind box of super primitive series will be launched soon, and platofarm will enable more rights and interests
![[ESP32][esp-idf] AP+STA实现无线桥接 中转wifi信号](/img/bf/0a968064a8f7c11b86a2a2820208e6.png)
[ESP32][esp-idf] AP+STA实现无线桥接 中转wifi信号

2021.07.13 我们是这样崩的

JS提升:实现flat平铺的底层原理
随机推荐
OSPF的LSA及优化
In hot weather, the line of defense for safe production was strengthened, and Guangzhou Haizhu District carried out emergency drills for gas stations
Can multithreading optimize program performance?
Edge团队详解如何通过磁盘缓存压缩技术提升综合性能体验
Standing on the shoulders of big men, you can see further
领域事件和集成事件没那么高大上
Branches and loops (1)
Weekly report on July 27, 2022
New features of ES6
The secret behind three salary increases a year
PHP 常用的数组整理
Mock.js
MySQL master-slave architecture. After the master database is suspended and restarted, how can the slave database automatically connect to the master database
博弈论 1.Introduction(组合游戏基本概念、对抗搜索、Bash游戏、Nim游戏)
SizeBasedTriggeringPolicy简介说明
FixedWindowRollingPolicy简介说明
Today, I want to talk about the data types of MySQL database
The victory of Dao community, tiger Dao VC wins in governance and consensus
老板:公司系统太多,能不能实现账号互通?
对象到对象映射-AutoMapper