-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathm1740 v1 Weekly Premium.py
48 lines (41 loc) · 1.31 KB
/
m1740 v1 Weekly Premium.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
39
40
41
42
43
44
45
46
47
48
# 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 findDistance(self, root: Optional[TreeNode], p: int, q: int) -> int:
path = [root]
pPath = []
qPath = []
def dfs(path: List[Optional[TreeNode]],
pPath: List[Optional[TreeNode]],
qPath: List[Optional[TreeNode]]) -> None :
if pPath and qPath :
return
if path[-1].val == p :
pPath.extend(path)
if path[-1].val == q :
qPath.extend(path)
if path[-1].left :
path.append(path[-1].left)
dfs(path, pPath, qPath)
path.pop()
if path[-1].right :
path.append(path[-1].right)
dfs(path, pPath, qPath)
path.pop()
dfs(path, pPath, qPath)
cnter = 0
while len(pPath) < len(qPath) :
cnter += 1
qPath.pop()
while len(pPath) > len(qPath) :
cnter += 1
pPath.pop()
while pPath[-1] != qPath[-1] :
cnter += 2
pPath.pop()
qPath.pop()
return cnter