13 lines Python


  • 0
    class Solution(object):
        def countUnivalSubtrees(self, root):
            """
            :type root: TreeNode
            :rtype: int
            """
            self.count = 0
            def dfs(root, pv):
                if not root:
                    return True
                left = dfs(root.left, root.val)
                right = dfs(root.right, root.val)
                if left and right:
                    self.count += 1
                    if root.val == pv:
                        return True
                return False
            if root:
                dfs(root, root.val)
            return self.count
    

Log in to reply
 

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