Problem Description
Given the root of a binary tree with unique values and a target integer k, find and return the value of the nearest leaf node to the target k. The "nearest" is defined in terms of the minimum number of edges traveled from the target node until a leaf node is reached. A leaf is any node with no children.
Key Insights
- Convert the binary tree into an undirected graph to allow traversal in all directions (to parent and children).
- Use depth-first search (DFS) to build the graph and simultaneously identify which nodes are leaves (nodes with no left and right children in the tree).
- Locate the target node (node with value k) during DFS.
- Perform a breadth-first search (BFS) from the target node in the graph to find the first encountered leaf, as BFS naturally finds the shortest path in an unweighted graph.
Space and Time Complexity
Time Complexity: O(n), where n is the number of nodes, since both DFS and BFS traverse all nodes in the worst case. Space Complexity: O(n), for storing the graph and BFS queue.
Solution
We first perform DFS on the binary tree to build a graph representation using an adjacency list. During this DFS, we also mark nodes as leaves based on whether they have any children. Once the graph is built and the target node is identified, we perform BFS from the target node. In BFS, the first node that meets the criteria of being a leaf (based on the original binary tree structure) is returned immediately. This approach guarantees that the path found is the shortest in terms of the number of edges traveled.