forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
number-of-good-leaf-nodes-pairs.py
75 lines (64 loc) · 2.44 KB
/
number-of-good-leaf-nodes-pairs.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
# Time: O(n)
# Space: O(h)
import collections
# Definition for a binary tree node.
class TreeNode(object):
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
class Solution(object):
def countPairs(self, root, distance):
"""
:type root: TreeNode
:type distance: int
:rtype: int
"""
def iter_dfs(distance, root):
result = 0
stk = [(1, (root, [collections.Counter()]))]
while stk:
step, params = stk.pop()
if step == 1:
node, ret = params
if not node:
continue
if not node.left and not node.right:
ret[0][0] = 1
continue
left, right = [collections.Counter()], [collections.Counter()]
stk.append((2, (left, right, ret)))
stk.append((1, (node.right, right)))
stk.append((1, (node.left, left)))
else:
left, right, ret = params
for left_d, left_c in left[0].iteritems():
for right_d,right_c in right[0].iteritems():
if left_d+right_d+2 <= distance:
result += left_c*right_c
ret[0] = collections.Counter({k+1:v for k,v in (left[0]+right[0]).iteritems()})
return result
return iter_dfs(distance, root)
# Time: O(n)
# Space: O(h)
import collections
class Solution2(object):
def countPairs(self, root, distance):
"""
:type root: TreeNode
:type distance: int
:rtype: int
"""
def dfs(distance, node):
if not node:
return 0, collections.Counter()
if not node.left and not node.right:
return 0, collections.Counter([0])
left, right = dfs(distance, node.left), dfs(distance, node.right)
result = left[0]+right[0]
for left_d, left_c in left[1].iteritems():
for right_d,right_c in right[1].iteritems():
if left_d+right_d+2 <= distance:
result += left_c*right_c
return result, collections.Counter({k+1:v for k,v in (left[1]+right[1]).iteritems()})
return dfs(distance, root)[0]