Skip to content

Latest commit

 

History

History
28 lines (23 loc) · 965 Bytes

965.-univalued-binary-tree.md

File metadata and controls

28 lines (23 loc) · 965 Bytes

965. Univalued Binary Tree

Solution 1

Runtime: 28 msMemory Usage: 14.3 MB

class Solution:
    def isUnivalTree(self, root: Optional[TreeNode]) -> bool:
        if not root:
            return True
        if root.left:
            if root.left.val!=root.val:
                return False
        if root.right:
            if root.right.val!=root.val:
                return False
        return self.isUnivalTree(root.left) and self.isUnivalTree(root.right)

{% hint style="info" %} Simple recursive method. We only have to return the 'False situations' instead considering all the correct ones. {% endhint %}