Deepest Leaves Sum (Leetcode 1302)
Hint: Level Order Traversal
Problem Link: https://leetcode.com/problems/deepest-leaves-sum/
class Solution:
def deepestLeavesSum(self, root: Optional[TreeNode]) -> int:
q=[root]
while(len(q)>0):
temp=[]
for i in range(len(q)):
node=q.pop()
if(node.left is not None):
q.insert(0,node.left)
if(node.right is not None):
q.insert(0,node.right)
temp.append(node.val)
return sum(temp)
PreviousAverage of Levels in a Binary Tree (Leetcode 637)NextFind the corresponding node of a binary tree in the clone of that tree (Leetcode 1379)
Last updated