DSA SectionMedium

Binary Tree

Detailed guide and Python implementation for the 'Binary Tree' problem.

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?
Consider using Trees-specific data structures like sets or heaps.
Edge Cases to Watch
  • Empty input structures
  • Single element inputs
  • Large numerical bounds

Ready to Solve?

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

Open in Editor

Recommended Python Resources

Expand your knowledge with related interactive tutorials, cheat sheets, and code comparisons.