[
tree
dfs
bfs
recursion
]
Leetcode 0965. Univalued Binary Tree
Problem statement
https://leetcode.com/problems/univalued-binary-tree/
Solution
Just use simple dfs.
Complexity
It is O(n)
for time and O(h)
for space.
Code
class Solution:
def isUnivalTree(self, root):
if not root: return True
if root.left and root.left.val != root.val: return False
if root.right and root.right.val != root.val: return False
return self.isUnivalTree(root.left) and self.isUnivalTree(root.right)