Binary Tree Paths

EasyTreesbacktracking

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

Given the root of a non-empty binary tree, list every path that starts at the root and ends at a leaf (a node with no children). Write each path as a string of the node values joined by ->, for example "1->2->5". Negative values keep their minus sign, as in "-1->-2". Return the paths in any order; the grader ignores the order of the list.

Examples

Input: root = [1,2,3,null,5]
Output: ["1->2->5","1->3"]
The leaves are 5 and 3.
Input: root = [1]
Output: ["1"]
The root is itself a leaf.

Constraints

  • 1 <= number of nodes <= 1000
  • -100 <= Node.val <= 100

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More backtracking problems