forked from Adam-Jimenez/binarysearch-editorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinary Tree Longest Consecutive Path.py
More file actions
33 lines (32 loc) · 1.08 KB
/
Binary Tree Longest Consecutive Path.py
File metadata and controls
33 lines (32 loc) · 1.08 KB
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
"""
Binary Tree Longest Consecutive Path
My solution is kinda fucked, but I transformed the tree into a undirected graph so it was possible to explore nodes in all directions.
"""
from collections import defaultdict
class Solution:
def solve(self, root):
adj_list=defaultdict(list)
def dfs(node):
for c in (node.left,node.right):
if not c: continue
adj_list[node].append(c)
adj_list[c].append(node)
dfs(c)
dfs(root)
seen=set()
def longest(node,diff=None):
seen.add(node)
l=0
for nei in adj_list[node]:
if nei in seen: continue
if diff is None:
if abs(node.val-nei.val)==1:
l=max(l,1+longest(nei,nei.val-node.val))
elif nei.val-node.val == diff:
l=max(l,1+longest(nei,nei.val-node.val))
seen.remove(node)
return l
ans=0
for node in adj_list:
ans=max(ans, longest(node))
return ans+1