-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathfind-root-of-n-ary-tree.py
39 lines (28 loc) · 1005 Bytes
/
find-root-of-n-ary-tree.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
from typing import List
"""
# Definition for a Node.
class Node:
def __init__(self, val=None, children=None):
self.val = val
self.children = children if children is not None else []
"""
class Solution:
def findRoot(self, tree: List['Node']) -> 'Node':
total = sum(node.val for node in tree)
for node in tree:
for child in node.children:
total -= child.val
return list(filter(lambda x: x.val == total, tree))[0]
def findRoot1(self, tree: List['Node']) -> 'Node':
min_value = min(node.val for node in tree)
for node in tree:
node.val = node.val - min_value + 1
for node in tree:
for child in node.children:
child.val = - child.val
root = list(filter(lambda x: x.val > 0, tree))[0]
root.val += min_value - 1
for node in tree:
if node != root:
node.val = - node.val + min_value - 1
return root