-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcount_nodes_equal_to_sum_of_descendants.py
94 lines (77 loc) · 2.77 KB
/
count_nodes_equal_to_sum_of_descendants.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
'''
Given the root of a binary tree, return the number of nodes where the value of the node is equal to the sum of the values of its descendants.
A descendant of a node x is any node that is on the path from node x to some leaf node. The sum is considered to be 0 if the node has no descendants.
'''
class Solution:
def equalToDescendants(self, root: Optional[TreeNode]) -> int:
def fn(node):
"""Return sum of nodes' value on sub-tree."""
nonlocal ans
if not node: return 0
sm = fn(node.left) + fn(node.right)
if sm == node.val: ans += 1
return sm + node.val
ans = 0
fn(root)
return ans
----------------------------------
def equalToDescendants(root):
"""
:type root: Optional[TreeNode]
:rtype: int
"""
from functools import lru_cache
@lru_cache(maxsize = None)
def get_sum_des(node):
# gives the sum of the node + its descendents - so if you want only sum of descendents you need to subtract the node.val itself - see *
if not node:
return 0
else:
return node.val + get_sum_des(node.right) + get_sum_des(node.left)
cnt = 0
stack = [root]
while stack:
node = stack.pop()
if get_sum_des(node) - node.val == node.val: # <- see *
cnt += 1
if node.left:
stack.append(node.left)
if node.right:
stack.append(node.right)
return cnt
--------------------------------------
def equalToDescendants(self, root: Optional[TreeNode]) -> int:
count = 0
def dfs(node=root):
if not node:
return 0
nonlocal count
sum_ = dfs(node.left)+dfs(node.right)
count += sum_ == node.val
return sum_+node.val
dfs()
return count
----------------------------------------------------
# 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 equalToDescendants(self, root: Optional[TreeNode]) -> int:
res = 0
def helper(root):
nonlocal res
if root == None:
return 0
if root.left == root.right == None:
res += 1 if root.val == 0 else 0
return root.val
left_sum = helper(root.left)
right_sum = helper(root.right)
if root.val == left_sum + right_sum:
res += 1
return right_sum + left_sum + root.val
helper(root)
return res