Back to course home
0% completed
Vote For New Content
Perhaps a cleaner and more readable approach, though it uses O(N) space.
Dakshesh Jain
Mar 15, 2025
class Solution: def isValidSerialization(self, preorder: str) -> bool: nodes = preorder.split(',') available_slots = 1 # for root node for node in nodes: # since node is processed available_slots -= 1 # negative slots can't be possible if available_slots < 0: return False # since this is not a null node it will create 2 slots if node != "#": available_slots += 2 # finally checking if all slots are exactly filled return available_slots == 0
1
0
Comments
Comments