Find the closest leaf to a target value in a binary tree

Quick Overview

Given a binary tree with unique values and a target value, return the leaf reachable from the target in the fewest edges, where paths may climb through parents, breaking ties by the smaller value. It tests turning a tree into an undirected graph and searching it layer by layer.

Find the closest leaf to a target value in a binary tree

Company: Databricks

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

You are given a binary tree in which every node has a unique value, and a target value `k`. Return the value of the leaf that is nearest to the node whose value is `k`. Distance between two nodes is the number of edges on the path between them, and that path may go up through parents as well as down through children. A leaf is a node with no children. ### Function Signature ```python from typing import List, Optional def find_closest_leaf(tree: List[Optional[int]], k: int) -> int: ... ``` ### Rules - `tree` is the level-order encoding of the tree. `tree[0]` is the root. After it, each non-null node, taken in order, uses the next two entries as its left and right child, and `None` marks a missing child. A `None` entry has no child slots of its own, and trailing `None` entries may be left out. - If several leaves are equally near, return the smallest value among them. - If the node with value `k` is itself a leaf, return `k` (distance 0). - A root that has one child is not a leaf. ### Constraints - `1 <= n <= 1000`, where `n` is the number of nodes in the tree - `1 <= Node.val <= 1000` - All node values are unique. - Some node in the tree has value `k`. ### Examples ```text Input: tree = [1,3,2], k = 1 Output: 2 ``` The target is the root. Both leaves, 3 and 2, are one edge away, and the tie goes to the smaller value. ```text Input: tree = [1,2,3,4,None,None,None,5,None,6], k = 2 Output: 3 ``` The root 1 has children 2 and 3, and a chain 2 -> 4 -> 5 -> 6 runs down the left side. Leaf 3 is two edges away (2 -> 1 -> 3), while leaf 6 is three edges away (2 -> 4 -> 5 -> 6). ```text Input: tree = [1,2,None,3], k = 1 Output: 3 ``` The tree is the chain 1 -> 2 -> 3. The root has one child, so it is not a leaf, and the only leaf is 3, two edges below it.

Overview: Given a binary tree with unique values and a target value, return the leaf reachable from the target in the fewest edges, where paths may climb through parents, breaking ties by the smaller value. It tests turning a tree into an undirected graph and searching it layer by layer.

Databricks logo
Databricks
Nov 25, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

You are given a binary tree in which every node has a unique value, and a target value k. Return the value of the leaf that is nearest to the node whose value is k.

Distance between two nodes is the number of edges on the path between them, and that path may go up through parents as well as down through children. A leaf is a node with no children.

Function Signature

from typing import List, Optional

def find_closest_leaf(tree: List[Optional[int]], k: int) -> int:
    ...

Rules

  • tree is the level-order encoding of the tree. tree[0] is the root. After it, each non-null node, taken in order, uses the next two entries as its left and right child, and None marks a missing child. A None entry has no child slots of its own, and trailing None entries may be left out.
  • If several leaves are equally near, return the smallest value among them.
  • If the node with value k is itself a leaf, return k (distance 0).
  • A root that has one child is not a leaf.

Constraints

  • 1 <= n <= 1000 , where n is the number of nodes in the tree
  • 1 <= Node.val <= 1000
  • All node values are unique.
  • Some node in the tree has value k .

Examples

Input: tree = [1,3,2], k = 1
Output: 2

The target is the root. Both leaves, 3 and 2, are one edge away, and the tie goes to the smaller value.

Input: tree = [1,2,3,4,None,None,None,5,None,6], k = 2
Output: 3

The root 1 has children 2 and 3, and a chain 2 -> 4 -> 5 -> 6 runs down the left side. Leaf 3 is two edges away (2 -> 1 -> 3), while leaf 6 is three edges away (2 -> 4 -> 5 -> 6).

Input: tree = [1,2,None,3], k = 1
Output: 3

The tree is the chain 1 -> 2 -> 3. The root has one child, so it is not a leaf, and the only leaf is 3, two edges below it.