Skip to content

Commit f5a49c2

Browse files
committed
Daily
1 parent 0cf8e30 commit f5a49c2

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

my-submissions/m2096 Daily.py

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode:
3+
# def __init__(self, val=0, left=None, right=None):
4+
# self.val = val
5+
# self.left = left
6+
# self.right = right
7+
class Solution:
8+
def getDirections(self, root: Optional[TreeNode], startValue: int, destValue: int) -> str:
9+
start, end = [], []
10+
11+
def dfs(curr: Optional[TreeNode], start: List[str], target: List[str], path: List[str] = []) -> None :
12+
if start and target :
13+
return
14+
15+
if curr.val == startValue :
16+
start.extend(path)
17+
if curr.val == destValue :
18+
target.extend(path)
19+
20+
if curr.left :
21+
path.append('L')
22+
dfs(curr.left, start, target, path)
23+
path.pop()
24+
if curr.right :
25+
path.append('R')
26+
dfs(curr.right, start, target, path)
27+
path.pop()
28+
29+
dfs(root, start, end)
30+
31+
shorter = min(len(start), len(end))
32+
ups = 0
33+
while ups < shorter :
34+
if start[ups] != end[ups] :
35+
break
36+
ups += 1
37+
38+
return 'U' * (len(start) - ups) + ''.join(end[ups:])

0 commit comments

Comments
 (0)