# 742     Closest Leaf in a Binary Tree

## 742. [Closest Leaf in a Binary Tree](https://leetcode.com/problems/closest-leaf-in-a-binary-tree/description/)

## 1. Question

Given a binary tree **where every node has a unique value**, and a target key`k`, find the value of the nearest leaf node to target`k`in the tree.

Here,nearest to a leaf means the least number of edges travelled on the binary tree to reach any leaf of the tree. Also, a node is called aleafif it has no children.

In the following examples, the input tree is represented in flattened form row by row. The actual`root`tree given will be a TreeNode object.

**Example 1:**

```
Input: root = [1, 3, 2], k = 1
Diagram of binary tree:
          1
         / \
        3   2


Output: 2 (or 3)

Explanation: Either 2 or 3 is the nearest leaf node to the target of 1.
```

**Example 2:**

```
Input: root = [1], k = 1

Output: 1


Explanation: The nearest leaf node is the root node itself.
```

**Example 3:**

```
Input: root = [1,2,3,4,null,null,null,5,null,6], k = 2
Diagram of binary tree:
             1
            / \
           2   3
          /
         4
        /
       5
      /
     6


Output: 3

Explanation:
The leaf node with value 3 (and not the leaf node with value 6) is nearest to the node with value 2.
```

**Note:**

1. `root`represents a binary tree with at least`1`node and at most`1000`nodes.
2. Every node has a unique`node.val`in range`[1, 1000]`.
3. There exists some node in the given binary tree for which`node.val == k`.

## 2. Implementation

**(1) DFS + BFS**

思路: 1. DFS to construct graph

1. BFS to find the closet leaf node

```java
class Solution {
    public int findClosestLeaf(TreeNode root, int k) {
        TreeNode[] start = new TreeNode[1];
        Map<TreeNode, List<TreeNode>> adjList = new HashMap<>();
        buildGraph(root, null, k, start, adjList);

        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(start[0]);

        Set<TreeNode> visited = new HashSet<>();

        while (!queue.isEmpty()) {
            int size = queue.size();

            for (int i = 0; i < size; i++) {
                TreeNode curNode = queue.remove();
                visited.add(curNode);

                if (curNode.left == null && curNode.right == null) {
                    return curNode.val;
                }

                for (TreeNode nextNode : adjList.get(curNode)) {
                    if (!visited.contains(nextNode)) {
                        queue.add(nextNode);
                    }
                }
            }
        }
        return -1;

    }

    public void buildGraph(TreeNode curNode, TreeNode parent, int k, TreeNode[] start, Map<TreeNode, List<TreeNode>> adjList) {
        if (curNode == null) {
            return;
        }

        if (curNode.val == k) {
            start[0] = curNode;
        }

        if (parent != null) {
            adjList.putIfAbsent(curNode, new ArrayList<>());
            adjList.putIfAbsent(parent, new ArrayList<>());
            adjList.get(curNode).add(parent);
            adjList.get(parent).add(curNode);
        }

        buildGraph(curNode.left, curNode, k, start, adjList);
        buildGraph(curNode.right, curNode, k, start, adjList);
    }
}
```

## 3. Time & Space Complexity

**DFS + BFS** : 时间复杂度O(n), 空间复杂度O(n)
