Re: [闲聊] 每日leetcode

楼主: DJYOMIYAHINA (通通打死)   2024-07-16 23:39:19
是只有肥肥被这题搞的要死吗?
吐了==
一次要肥肥写我不会写的LCA
还要肥肥处理python DFS字串concate遇到的MLE
我真的不会写程式:(
def getDirections(self, root: Optional[TreeNode], startValue: int, destValue:
int) -> str:
LCA = None
def findLCA(root) -> bool:
nonlocal LCA
if root is None:
return False
left_contains = findLCA(root.left)
right_contains = findLCA(root.right)
current_is_target = root.val == startValue or root.val == destValue
if current_is_target and (left_contains or right_contains):
LCA = root
return True
if left_contains and right_contains:
LCA = root
return True
return current_is_target or left_contains or right_contains
findLCA(root)
if LCA is None:
return ""
def findPath(root, target, path):
if root is None:
return False
if root.val == target:
return True
path.append("L")
if findPath(root.left, target, path):
return True
path.pop()
path.append("R")
if findPath(root.right, target, path):
return True
path.pop()
return False
start_path = []
dest_path = []
findPath(LCA, startValue, start_path)
findPath(LCA, destValue, dest_path)
return "U" * len(start_path) + "".join(dest_path)
作者: nozomizo (希霙)   2024-07-16 23:57:00
大师

Links booklink

Contact Us: admin [ a t ] ucptt.com