Skip to content

742. Closest Leaf in a Binary Tree 👍

  • Time: $O(n)$
  • Space: $O(n)$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
class Solution {
 public:
  int findClosestLeaf(TreeNode* root, int k) {
    int ans = -1;
    int minDist = 1000;
    // {node: distance to TreeNode(k)}
    unordered_map<TreeNode*, int> nodeToDist;

    getDists(root, k, nodeToDist);
    getClosestLeaf(root, 0, nodeToDist, minDist, ans);

    return ans;
  }

 private:
  void getDists(TreeNode* root, int k,
                unordered_map<TreeNode*, int>& nodeToDist) {
    if (root == nullptr)
      return;
    if (root->val == k) {
      nodeToDist[root] = 0;
      return;
    }

    getDists(root->left, k, nodeToDist);
    if (const auto it = nodeToDist.find(root->left); it != nodeToDist.cend()) {
      // The TreeNode(k) is in the left subtree.
      nodeToDist[root] = it->second + 1;
      return;
    }

    getDists(root->right, k, nodeToDist);
    if (const auto it = nodeToDist.find(root->right); it != nodeToDist.cend())
      // The TreeNode(k) is in the right subtree.
      nodeToDist[root] = it->second + 1;
  }

  void getClosestLeaf(TreeNode* root, int dist,
                      unordered_map<TreeNode*, int>& nodeToDist, int& minDist,
                      int& ans) {
    if (root == nullptr)
      return;
    if (nodeToDist.contains(root))
      dist = nodeToDist[root];
    if (root->left == nullptr && root->right == nullptr) {
      if (dist < minDist) {
        minDist = dist;
        ans = root->val;
      }
      return;
    }

    getClosestLeaf(root->left, dist + 1, nodeToDist, minDist, ans);
    getClosestLeaf(root->right, dist + 1, nodeToDist, minDist, ans);
  }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
class Solution {
  public int findClosestLeaf(TreeNode root, int k) {
    ans = -1;
    minDist = 1000;
    // {node: distance to TreeNode(k)}
    Map<TreeNode, Integer> nodeToDist = new HashMap<>();

    getDists(root, k, nodeToDist);
    getClosestLeaf(root, 0, nodeToDist);

    return ans;
  }

  private int ans;
  private int minDist;

  private void getDists(TreeNode root, int k, Map<TreeNode, Integer> nodeToDist) {
    if (root == null)
      return;
    if (root.val == k) {
      nodeToDist.put(root, 0);
      return;
    }

    getDists(root.left, k, nodeToDist);
    if (nodeToDist.containsKey(root.left)) {
      // The TreeNode(k) is in the left subtree.
      nodeToDist.put(root, nodeToDist.get(root.left) + 1);
      return;
    }

    getDists(root.right, k, nodeToDist);
    if (nodeToDist.containsKey(root.right))
      // The TreeNode(k) is in the right subtree.
      nodeToDist.put(root, nodeToDist.get(root.right) + 1);
  }

  private void getClosestLeaf(TreeNode root, int dist, Map<TreeNode, Integer> nodeToDist) {
    if (root == null)
      return;
    if (nodeToDist.containsKey(root))
      dist = nodeToDist.get(root);
    if (root.left == null && root.right == null) {
      if (dist < minDist) {
        minDist = dist;
        ans = root.val;
      }
      return;
    }

    getClosestLeaf(root.left, dist + 1, nodeToDist);
    getClosestLeaf(root.right, dist + 1, nodeToDist);
  }
}