leetcode -. 965 single binary value

class Solution:
    def isUnivalTree(self, root: TreeNode) -> bool:
        if not root:
            return True
        else:
            a=root.val
            if root.left and root.left.val!=a:
                return False
            elif root.right and root.right.val!=a:
                return False
            else:
                return self.isUnivalTree(root.left) and self.isUnivalTree(root.right)
When execution: 44 ms, beat the 75.90% of users in all python3 submission
Memory consumption: 13.8 MB, beat the 5.47% of users in all python3 submission
 
——2019.11.21

Guess you like

Origin www.cnblogs.com/taoyuxin/p/11907541.html