当前位置:网站首页>Xiao Hei stands up again and looks at leetcode:653. Sum of two IV - enter BST
Xiao Hei stands up again and looks at leetcode:653. Sum of two IV - enter BST
2022-07-28 10:09:00 【Xiaohei invincible】
Little black answer
# 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

Depth first
# 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)

Width first
# 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

Depth-first search + In the sequence traversal + Double pointer
# 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

iteration + In the sequence traversal
# 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

边栏推荐
- Install lamp under deepin
- 深度学习必懂的 13 种概率分布
- LandingSite电子标签Quuppa固件进入DFU状态说明
- Flink - checkpoint Failure reason: Not all required tasks are currently running
- JWT 登录认证 + Token 自动续期方案,写得太好了!
- In retaliation for the dismissal of the company, I changed all code comments of the project!
- 【MySQL】查询多个ID返回字符串拼接
- [openharmony] [rk2206] build openharmony compiler (2)
- 每天在岗不足8小时被辞?腾讯前员工追讨1300万加班费等,法院终审获赔9万
- [OpenHarmony] [RK2206] 构建OpenHarmony编译器 (二)
猜你喜欢

广州地铁14号线新市墟站开建,白云区居民即将开启双线换乘模式!

Have you ever seen this kind of dynamic programming -- the stock problem of state machine dynamic programming (Part 2)

OSPF expansion configuration, routing principles, anti ring and re release

The blind box of super primitive series will be launched soon, and platofarm will enable more rights and interests

Software testing and quality learning notes 2 - black box testing

高温持续,公交企业开展安全专项培训

arthas使用教程
Edge team explains how to improve the comprehensive performance experience through disk cache compression technology

在Plato Farm新经济模型下,如何在游戏中获取更多MARK

极致通缩和永动机模型,将推动 PlatoFarm 爆发
随机推荐
Tencent technical experts: decrypt the 100 million user products wechat, QQ, King glory... Comprehensively practice on the cloud!
Have you ever seen this kind of dynamic programming -- the stock problem of state machine dynamic programming (Part 2)
Array collation commonly used in PHP
ConsoleAppender简介说明
OSPF expansion configuration, routing principles, anti ring and re release
Introduction to evaluatorfilter
并查集
工业品MRO采购网站有哪些优势?一文带你读懂
arthas使用教程
能够遍历一个文件夹下的所有文件和子文件夹
医药行业数字化建设,箭在弦上
Installing MySQL for Linux operating system (centos7)
居家健康诊断时代下,Senzo打造增强侧向流测试产品
LSA and optimization of OSPF
PHP7 中 ?? 与? :的区别
二分、三分、01分数规划 【第I弹】
Bit.store, which has attracted much attention, is at a glance of the latest developments
SkiaSharp 之 WPF 自绘 拖曳小球(案例版)
In hot weather, the line of defense for safe production was strengthened, and Guangzhou Haizhu District carried out emergency drills for gas stations
PHP 基础