LeetCode Solutions

863. All Nodes Distance K in Binary Tree

Time: $O(n)$

Space: $O(n)$

			

class Solution {
 public:
  vector<int> distanceK(TreeNode* root, TreeNode* target, int K) {
    vector<int> ans;
    unordered_map<TreeNode*, int> nodeToDist;  // {node: distance to target}

    getDists(root, target, nodeToDist);
    dfs(root, K, 0, nodeToDist, ans);
    return ans;
  }

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

    getDists(root->left, target, nodeToDist);
    if (nodeToDist.count(root->left)) {
      // The target is in the left subtree
      nodeToDist[root] = nodeToDist[root->left] + 1;
      return;
    }

    getDists(root->right, target, nodeToDist);
    if (nodeToDist.count(root->right))
      // The target is in the right subtree
      nodeToDist[root] = nodeToDist[root->right] + 1;
  }

  void dfs(TreeNode* root, int K, int dist,
           unordered_map<TreeNode*, int>& nodeToDist, vector<int>& ans) {
    if (root == nullptr)
      return;
    if (nodeToDist.count(root))
      dist = nodeToDist[root];
    if (dist == K)
      ans.push_back(root->val);

    dfs(root->left, K, dist + 1, nodeToDist, ans);
    dfs(root->right, K, dist + 1, nodeToDist, ans);
  }
};
			

class Solution {
  public List<Integer> distanceK(TreeNode root, TreeNode target, int K) {
    List<Integer> ans = new ArrayList<>();
    Map<TreeNode, Integer> nodeToDist = new HashMap<>(); // {node: distance to target}

    getDists(root, target, nodeToDist);
    dfs(root, K, 0, nodeToDist, ans);

    return ans;
  }

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

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

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

  private void dfs(TreeNode root, int K, int dist, Map<TreeNode, Integer> nodeToDist,
                   List<Integer> ans) {
    if (root == null)
      return;
    if (nodeToDist.containsKey(root))
      dist = nodeToDist.get(root);
    if (dist == K)
      ans.add(root.val);

    dfs(root.left, K, dist + 1, nodeToDist, ans);
    dfs(root.right, K, dist + 1, nodeToDist, ans);
  }
}