Tap here to switch tabs
Problem
Submissions

Problem: Closest Leaf in a Binary Tree

Closest Leaf in a Binary Tree asks you to find the value of a leaf with no children nearest to the node containing k by edge distance; any tie is valid.

Statement

Given the root of a binary tree where each node has a unique value, and an integer k, find the value of the leaf node that is closest to the node containing the value k.

A leaf node is a node that has no children. The distance between any two nodes is the number of edges along the path connecting them. If multiple leaf nodes are at the same minimum distance from the target node, returning any of their values is valid.

Constraints:

  • The number of nodes in the tree is in the range [1,1000][1, 1000].

  • 1≤1 \leq Node.val ≤1000\leq 1000

  • All values in the tree are unique.

  • There exists at least one node in the tree where Node.val =k= k.

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Closest Leaf in a Binary Tree

Closest Leaf in a Binary Tree asks you to find the value of a leaf with no children nearest to the node containing k by edge distance; any tie is valid.

Statement

Given the root of a binary tree where each node has a unique value, and an integer k, find the value of the leaf node that is closest to the node containing the value k.

A leaf node is a node that has no children. The distance between any two nodes is the number of edges along the path connecting them. If multiple leaf nodes are at the same minimum distance from the target node, returning any of their values is valid.

Constraints:

  • The number of nodes in the tree is in the range [1,1000][1, 1000].

  • 1≤1 \leq Node.val ≤1000\leq 1000

  • All values in the tree are unique.

  • There exists at least one node in the tree where Node.val =k= k.