PracHub
QuestionsCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates a candidate's ability to work with binary tree traversal and path-based numerical aggregation, testing competency in tree data structures and algorithmic reasoning within the Coding & Algorithms domain.

  • medium
  • Meta
  • Coding & Algorithms
  • Software Engineer

Sum numbers formed by root-to-leaf paths

Company: Meta

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

You are given the root of a binary tree where each node contains a single digit from 0 to 9. Each root-to-leaf path represents a number obtained by concatenating the digits along the path from the root down to a leaf. For example, if a path is root → 1 → 2 → 3, it represents the number 123. Write a function that returns the sum of all numbers formed by all root-to-leaf paths in the tree. - Input: the root node of a binary tree. - Output: an integer representing the sum of all root-to-leaf numbers. Constraints: - Each node value is a digit from 0 to 9. - The tree has at most 1000 nodes. Describe your algorithm and its time and space complexity. You do not need to provide actual code.

Quick Answer: This question evaluates a candidate's ability to work with binary tree traversal and path-based numerical aggregation, testing competency in tree data structures and algorithmic reasoning within the Coding & Algorithms domain.

Given a binary tree in level-order form, sum the numbers represented by each root-to-leaf digit path.

Constraints

  • Each node value is a digit 0..9; None represents a missing node

Examples

Input: ([1, 2, 3],)

Expected Output: 25

Explanation: 12 + 13.

Input: ([4, 9, 0, 5, 1],)

Expected Output: 1026

Explanation: 495 + 491 + 40.

Input: ([],)

Expected Output: 0

Explanation: Empty tree.

Input: ([0, 1],)

Expected Output: 1

Explanation: Leading zero path.

Hints

  1. Carry the current prefix value during DFS.
Last updated: Jun 27, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • AI Coding Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Find Shortest Unique Prefixes - Meta (medium)
  • Compute Exclusive Execution Times - Meta (medium)
  • Solve Tree Columns And Maze Variants - Meta (medium)
  • Solve Tree Diameter and Palindromic Counts - Meta (medium)
  • Simulate Monster Team Battles - Meta (hard)