Back to Practice Dashboard
DSA SectionMedium

Binary Tree

Learn how to solve the 'Binary Tree' problem. This detailed resource details brute force and optimized approaches.

Problem Statement

Medium

Write a function is_univalued_binary_tree(tree_arr) that takes an array representation of a binary tree tree_arr (where the root is at index 0, and children of index i are at 2i + 1 and 2i + 2, with None representing empty nodes) and returns True if every node in the tree has the same value, or False otherwise.

Constraints
  • 0 <= len(tree_arr) <= 1000

Examples

Example 1
Input
tree_arr = [1, 1, 1, 1, 1, None, 1]
Output
True
Explanation

All nodes in the tree contain the value 1.

Example 2
Input
tree_arr = [2, 2, 2, 5, 2]
Output
False
Explanation

One node has value 5, which differs from other nodes (value 2).

Need a Hint?
Perform a recursive tree traversal (DFS) or level-order traversal (BFS) using a queue/stack.
Edge Cases to Watch
  • Empty list or null input variables
  • Single item lists/arrays
  • Extremely large input bounds causing integer or stack overflow

Ready to Solve?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

Open in Editor