Problem statement

https://binarysearch.com/problems/Sum-of-Right-Leaves/

Solution

Equal to Leetcode 0404. Sum of Left Leaves, but right instead of left.

Complexity

It is O(n) for time and O(h) for space.

Code

class Solution:
    def dfs(self, root, side):
        if not root: return
        
        if not root.left and not root.right:
            if side == 1: self.sum += root.val
        
        self.dfs(root.left, -1)
        self.dfs(root.right, 1)
    
    def solve(self, root):
        self.sum = 0
        self.dfs(root, 0)
        return self.sum