↓Skip to main content
  1. LeetCode/

LeetCode 965: Univalued Binary Tree

·1 min· ·
LeetCode Easy Tree Depth-First-Search Breadth-First-Search Binary-Tree
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: easy First Attempt: 2022-09-26

  • Submissions: Wrong Answer ×3 → Runtime Error ×2 → Wrong Answer ×2 → Runtime Error → Wrong Answer ×2 → Accepted → Runtime Error → Wrong Answer → Runtime Error → Accepted → Runtime Error → Accepted ×2

Approach
#

Every node must match the root’s value (Tree Template 1). Recursively: this node equals the target, and both subtrees do too. An empty subtree violates nothing and returns True.

My Attempts
#

This took 18 submissions, most with the same bug: node.val == self.isUnivalTree(node.left) == self.isUnivalTree(node.right) compared the node’s value with the booleans returned by the recursion. For a single node [2] both sides return True, and 2 == True fails, so it returned False. Passing the target value instead then hit a few undefined variables.

Solution
#

class Solution:
    def isUnivalTree(self, root: Optional[TreeNode]) -> bool:
        def same(node):
            return not node or (node.val == root.val and same(node.left) and same(node.right))
        return same(root)

Complexity
#

Time O(n), space O(h).

Mistakes to Avoid
#

  • The recursion returns booleans; do not compare them with node values.

Interview Explanation
#

I recursively check that every node equals the root’s value, treating empty subtrees as fine, and return True only if all pass. Time O(n).

Related

LeetCode 993: Cousins in Binary Tree
·1 min
LeetCode Easy Tree Depth-First-Search Breadth-First-Search Binary-Tree
LeetCode 993 note: cousins share a depth but not a parent; record x’s and y’s depth and parent during DFS and compare
LeetCode 637: Average of Levels in Binary Tree
·1 min
LeetCode Easy Tree Depth-First-Search Breadth-First-Search Binary-Tree
LeetCode 637 note: level-order traversal, dividing each level’s sum by its count
LeetCode 404: Sum of Left Leaves
·1 min
LeetCode Easy Tree Depth-First-Search Breadth-First-Search Binary-Tree
LeetCode 404 note: tell each child during DFS whether it is a left child, adding only left children that are leaves
LeetCode 226: Invert Binary Tree
·1 min
LeetCode Easy Tree Depth-First-Search Breadth-First-Search Binary-Tree
LeetCode 226 note: swap every node’s children, using a simultaneous assignment so the already-changed side is not reused
LeetCode 111: Minimum Depth of Binary Tree
·1 min
LeetCode Easy Tree Depth-First-Search Breadth-First-Search Binary-Tree
LeetCode 111 note: minimum depth must end at a leaf; with only one child, the empty side (depth 0) cannot count as the minimum
LeetCode 112: Path Sum
·1 min
LeetCode Easy Tree Depth-First-Search Breadth-First-Search Binary-Tree
LeetCode 112 note: subtract each value from targetSum on the way down; it works if the remainder matches at a leaf