Count median in binary tree

Last updated: November 27, 2025

Quick Overview

Given a binary tree, write a function to calculate the median of the values stored in the nodes. The median is defined as the middle value when the values are sorted in ascending order; if there is an even number of values, return the average of the two middle values. Your function should take the root of the binary tree as input and return the median as a float.

DoorDash
Coding & Algorithms
Software Engineer
DoorDash
November 27, 2025
Software Engineer
Technical Screen
Coding & Algorithms
Medium

20

2

1,649 solved


Given a binary tree, write a function to calculate the median of the values stored in the nodes. The median is defined as the middle value when the values are sorted in ascending order; if there is an even number of values, return the average of the two middle values. Your function should take the root of the binary tree as input and return the median as a float.

Coding interviews at DoorDash focus on problem-solving approach as much as the final solution. The interviewer wants to see you break down the problem, consider edge cases, and optimize iteratively. Communication throughout the process is key.

What the Interviewer Expects
  • Recognize the underlying problem pattern (sliding window, two pointers, BFS/DFS, etc.)
  • Discuss multiple approaches and trade-offs before coding
  • Implement an optimal solution with clean, production-quality code
  • Handle all edge cases including boundary conditions and invalid input
  • Optimize both time and space complexity with clear justification
  • Test your solution systematically with well-chosen examples
Key Topics to Cover
Time and space complexity analysis
Graph algorithms and traversal
Sorting and searching
Data structure selection and trade-offs
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • Can you solve this in a single pass?
  • What is the worst-case input for your solution?
  • How would your solution change if the input was sorted?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Problem Analysis

To find the median of values in a binary tree, we need to consider the properties of both binary trees and the definition of median. The binary tree can contain an arbitrary number of nodes, which mea...

Approach
  1. In-Order Traversal: We will use depth-first search (DFS) through in-order traversal to collect all node values into a list.
  2. Sorting the Values: After obtaining the list of values, we wi...

Submit Your Answer
Markdown supported

Related Questions