Binary Tree Paths
Problem Statement
You are given the root of a binary tree.
Return every root-to-leaf path as a string. Put -> between two values, for example 1->2->5. Do not put an arrow before the first value or after the last one. A leaf is a node with no children.
The root of a binary tree. It has at least one node.
A list of strings, one for each root-to-leaf path.
Node 2 has only a right child, so the path continues to 5. Node 3 is a leaf, so the path 1 → 3 ends there.
The root is also a leaf, so its only path is just its value.
- • The tree has between 1 and 100 nodes.
- • −100 ≤ Node.val ≤ 100
Pattern Recognition Clues
Recognising these key signals in the problem description immediately identifies the pattern:
Approach Complexity Ladder
Pass the path string by value, so every call works on its own copy
Push a value on the way down, pop it on the way up, and build a string only at leaves
Pass the string built so far as a parameter. Each call adds its node's value, then gives each child its own copy with -> added. Because each call has its own string, nothing has to be undone. The cost is in the copies.
Recognition rule for this sub-pattern:
Unlock Complete Visual Walkthrough & Optimal C++
The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.
First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.
How this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like Tree DFS translate into production architectures and low-level memory mechanics.
Foundations: Object-Oriented Principles in C++23
Class invariants, pure virtual interfaces, and RAII smart pointers.
Food Delivery System (Zomato Architecture)
Real-world machine coding application of algorithms and design patterns.
Pointers vs References Deep Dive
Pointer arithmetic and memory layout under the hood.
Virtual Method Table (vtable) & Memory Layout
Polymorphism, vptr overhead, and multiple inheritance in memory.