-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1302_deepestLeavesSum.py
51 lines (33 loc) · 1.07 KB
/
1302_deepestLeavesSum.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
# 1302. Deepest Leaves Sum
# 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
def maxDepth(root, depth):
if not root:
return depth
left = 1+maxDepth(root.left, depth)
right = 1+maxDepth(root.right, depth)
depth = max(left, right)
return depth
def findSum(root, depth, sum_leaves):
if root and depth==1:
sum_leaves+=root.val
return sum_leaves
if not root and depth>1:
return 0
if root:
depth-=1
left = findSum(root.left, depth, sum_leaves)
right = findSum(root.right, depth, sum_leaves)
sum_leaves = left+right
return sum_leaves
class Solution:
def deepestLeavesSum(self, root: Optional[TreeNode]) -> int:
depth = 0
depth = maxDepth(root, depth)
sumLeaves = 0
sumLeaves = findSum(root, depth, sumLeaves)
return sumLeaves