Updating exercises

This commit is contained in:
Carlos
2025-07-14 10:09:13 -04:00
parent 57956bf728
commit 7f1381ae93
25 changed files with 634 additions and 13 deletions

View File

@@ -0,0 +1,72 @@
# @leet imports start
from string import *
from re import *
from datetime import *
from collections import *
from heapq import *
from bisect import *
from copy import *
from math import *
from random import *
from statistics import *
from itertools import *
from functools import *
from operator import *
from io import *
from sys import *
from json import *
from builtins import *
import string
import re
import datetime
import collections
import heapq
import bisect
import copy
import math
import random
import statistics
import itertools
import functools
import operator
import io
import sys
import json
from typing import *
# @leet imports end
# @leet start
# 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 deepestLeavesSum(self, root: Optional[TreeNode]) -> int:
self.max_dep = 0
self.sum_max = 0
def dfs(node: TreeNode, depth: int):
if not node:
return
if not node.left and not node.right:
if depth > self.max_dep:
self.max_dep = depth
self.sum_max = node.val
elif depth == self.max_dep:
self.sum_max += node.val
else:
dfs(node.left, depth + 1)
dfs(node.right, depth + 1)
dfs(root, 0)
return self.sum_max
# @leet end