Encode Every Root-to-Leaf Path of a Binary Tree as a Left/Right Bit String

Quick Overview

Return a bit string for every root-to-leaf path of a binary tree, writing 0 for each move to a left child and 1 for each move to a right child, in left-to-right depth-first order. It tests tree traversal, carrying path state through a DFS, and edge cases such as an empty tree or a lone root.

Encode Every Root-to-Leaf Path of a Binary Tree as a Left/Right Bit String

Company: Two Sigma

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

Given the `root` of a binary tree, return a list of binary-encoded strings representing every root-to-leaf path. For each path, append `'0'` when moving to the left child and `'1'` when moving to the right child. The encoding does not include any node values; it only records the sequence of moves. Return the encodings in the order they are discovered during a left-to-right depth-first traversal of the tree. If the tree contains only the root (no children), return `[""]`. If the tree is empty, return an empty list. ### Function Signature ```python def encode_paths(root: list[int | None]) -> list[str]: ``` ### Rules - The tree is given as a level-order list in which `None` marks a missing child. Children of a missing node are not listed, and trailing `None` values may be left out. For example, `[1,None,2,3]` is a root with only a right child `2`, whose left child is `3`. - A leaf is a node with no left child and no right child. A node with exactly one child is not a leaf. - An empty list `[]` is the empty tree. - The output lists the encodings in left-to-right depth-first order: every leaf in a node's left subtree comes before every leaf in its right subtree. ### Constraints - The number of nodes in the tree is in the range `[0, 10^4]`. - `-10^9 <= Node.val <= 10^9` ### Examples ```text Input: root = [1,2,3,4,5,None,6] Output: ["00","01","11"] ``` The root `1` has children `2` and `3`, node `2` has children `4` and `5`, and node `3` has only a right child `6`. The paths `1->2->4`, `1->2->5` and `1->3->6` encode as `"00"`, `"01"` and `"11"`, in that left-to-right order. ```text Input: root = [1] Output: [""] ``` The root is itself a leaf, and reaching it takes no moves, so its encoding is the empty string. ```text Input: root = [1,2,3,None,4,5] Output: ["01","10"] ``` Node `2` has only a right child `4`, and node `3` has only a left child `5`. Nodes `2` and `3` are not leaves, so the only paths are `1->2->4` (left, then right) and `1->3->5` (right, then left).

Overview: Return a bit string for every root-to-leaf path of a binary tree, writing 0 for each move to a left child and 1 for each move to a right child, in left-to-right depth-first order. It tests tree traversal, carrying path state through a DFS, and edge cases such as an empty tree or a lone root.

Two Sigma logo
Two Sigma
Nov 24, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Given the root of a binary tree, return a list of binary-encoded strings representing every root-to-leaf path.

For each path, append '0' when moving to the left child and '1' when moving to the right child. The encoding does not include any node values; it only records the sequence of moves.

Return the encodings in the order they are discovered during a left-to-right depth-first traversal of the tree. If the tree contains only the root (no children), return [""]. If the tree is empty, return an empty list.

Function Signature

def encode_paths(root: list[int | None]) -> list[str]:

Rules

  • The tree is given as a level-order list in which None marks a missing child. Children of a missing node are not listed, and trailing None values may be left out. For example, [1,None,2,3] is a root with only a right child 2 , whose left child is 3 .
  • A leaf is a node with no left child and no right child. A node with exactly one child is not a leaf.
  • An empty list [] is the empty tree.
  • The output lists the encodings in left-to-right depth-first order: every leaf in a node's left subtree comes before every leaf in its right subtree.

Constraints

  • The number of nodes in the tree is in the range [0, 10^4] .
  • -10^9 <= Node.val <= 10^9

Examples

Input: root = [1,2,3,4,5,None,6]
Output: ["00","01","11"]

The root 1 has children 2 and 3, node 2 has children 4 and 5, and node 3 has only a right child 6. The paths 1->2->4, 1->2->5 and 1->3->6 encode as "00", "01" and "11", in that left-to-right order.

Input: root = [1]
Output: [""]

The root is itself a leaf, and reaching it takes no moves, so its encoding is the empty string.

Input: root = [1,2,3,None,4,5]
Output: ["01","10"]

Node 2 has only a right child 4, and node 3 has only a left child 5. Nodes 2 and 3 are not leaves, so the only paths are 1->2->4 (left, then right) and 1->3->5 (right, then left).