Python Solution

  • 0
    class Solution(object):
        def countUnivalSubtrees(self, root):
            self.count = 0
            return self.count
        def helper(self, root):
            if root == None:
                return True
            left, right = self.helper(root.left), self.helper(root.right)
            if left and right:
                if (not root.left or root.left.val == root.val) and (not root.right or root.right.val == root.val):
                    self.count += 1
                    return True
            return False

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.