Weekly Contest 419 All Solutions |

  Рет қаралды 483

Arnab Bhadra

Arnab Bhadra

Күн бұрын

Пікірлер: 2
@ArnabBhadra02
@ArnabBhadra02 3 ай бұрын
Soln 2: class TreeNode: def __init__(self, val=0, left=None, right=None): self.val = val self.left = left self.right = right class Solution: def __init__(self): self.perfect_subtree_sizes = [] def isPerfect(self, root): # Base case: if the node is null, it's a perfect subtree of size 0 if not root: return True, 0 # Check left and right subtrees recursively left_perfect, left_size = self.isPerfect(root.left) right_perfect, right_size = self.isPerfect(root.right) # If both subtrees are perfect and have the same size, the current subtree is perfect if left_perfect and right_perfect and left_size == right_size: size = 1 + left_size + right_size # Add 1 for the root self.perfect_subtree_sizes.append(size) return True, size # If not perfect, return False and size 0 return False, 0 def kthLargestPerfectSubtree(self, root, k: int) -> int: # First pass to fill up the list of perfect subtree sizes self.isPerfect(root) # Sort sizes in descending order self.perfect_subtree_sizes.sort(reverse=True) # If there are at least k perfect subtrees, return the k-th largest, otherwise return -1 if len(self.perfect_subtree_sizes) >= k: return self.perfect_subtree_sizes[k - 1] return -1
@gungunkulchandani
@gungunkulchandani 3 ай бұрын
solution 3 and 4 please
I Solved 100 LeetCode Problems
13:11
Green Code
Рет қаралды 319 М.
Maximum Width Ramp - Leetcode 962 - Python
14:47
NeetCodeIO
Рет қаралды 18 М.
Мясо вегана? 🧐 @Whatthefshow
01:01
История одного вокалиста
Рет қаралды 7 МЛН
Zig for the Uninitiated: Pointers, Arrays, and Slices
19:03
Tyler Calder
Рет қаралды 4,2 М.
How to ACTUALLY Learn to Code in 2024
17:14
Natassha Selvaraj
Рет қаралды 6 М.
ASMR Programming - Spinning Cube - No Talking
20:45
Servet Gulnaroglu
Рет қаралды 4,3 МЛН
Understand Assembly low-level programming in 22 minutes
22:08
Alisa Esage Шевченко
Рет қаралды 7 М.
Shifting Letters II  |  Leetcode DSA questions | Uncoding-Rohit
10:44
UnCoding - Rohit
Рет қаралды 10