-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathm2096 Daily.py
38 lines (32 loc) · 1.17 KB
/
m2096 Daily.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# 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 getDirections(self, root: Optional[TreeNode], startValue: int, destValue: int) -> str:
start, end = [], []
def dfs(curr: Optional[TreeNode], start: List[str], target: List[str], path: List[str] = []) -> None :
if start and target :
return
if curr.val == startValue :
start.extend(path)
if curr.val == destValue :
target.extend(path)
if curr.left :
path.append('L')
dfs(curr.left, start, target, path)
path.pop()
if curr.right :
path.append('R')
dfs(curr.right, start, target, path)
path.pop()
dfs(root, start, end)
shorter = min(len(start), len(end))
ups = 0
while ups < shorter :
if start[ups] != end[ups] :
break
ups += 1
return 'U' * (len(start) - ups) + ''.join(end[ups:])