FastPrepFarthest Distance From a Node in a Binary Tree
Problems/Farthest Distance From a Node in a Binary Tree

Farthest Distance From a Node in a Binary Tree

Google logoGoogle● MediumNEW GRADPHONE SCREEN

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: The author recalls a binary tree root, any target node, and the farthest distance you can reach from that node. We chose that values are unique, that distance counts edges in both directions, that the target is given as an integer, the examples, and the input limits. Our version is about 88% the same as the reported question.

You are given the root of a binary tree and the unique value target of one node in that tree.

You may move along each parent-child edge in either direction. Find the longest path from the target node to any node without repeating a node. Return the number of edges on that path.

Function

farthestDistance(root: TreeNode, target: int) → int

Examples

Example 1

root = [1,2,3,4,5,null,6]target = 2return = 3
root
124536

The farthest node is 6, reached along 2 - 1 - 3 - 6.

Example 2

root = [7]target = 7return = 0
root
7

The target is the only node.

Example 3

root = [1,2,null,3,null,4]target = 3return = 2
root
1234

From target 3, node 1 is two edges away along 3 - 2 - 1, while node 4 is one edge away. The maximum distance is 2.

Constraints

  • The tree contains from 1 through 200000 nodes.
  • Every node value is a signed 32-bit integer.
  • All node values are unique.
  • target occurs in the tree.

More Google Phone Screen questions

  • Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Periodic-Color Binary Forest RootsPHONE SCREEN · Seen Nov 2026
  • Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Best Stock Trade with Buy and Sell DaysPHONE SCREEN · Seen Nov 2026
  • Word Chain Jumps in a GridPHONE SCREEN · Seen Nov 2026
  • Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Apartment Housemate PreferencesPHONE SCREEN · Seen Nov 2026
  • Price at Query TimestampsPHONE SCREEN · Seen Nov 2026
  • Course Schedule Cycle DetectionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026

Similar questions at other companies

  • Distance Between Two Tree NodesAmazon · PHONE SCREEN · ONSITE INTERVIEW
  • Tree DiameterQualcomm · PHONE SCREENQualcomm interview questions
  • Frog Position After T SecondsInMobi · PHONE SCREENInMobi interview questions
  • Hubs With the Shortest Longest ChainAdobe · PHONE SCREENAdobe interview questions
  • Tree Pythagorean TriplesIBM · OAIBM interview questions
  • Diameter of an Acyclic Undirected GraphSalesforce · ONSITE INTERVIEWSalesforce interview questions
  • Binary Tree Nodes at Distance KApple · ONSITE INTERVIEWApple interview questions
  • Find the Tree DiameterAdyen · ONSITE INTERVIEWAdyen interview questions
  • Binary Tree Nodes at Distance KGoldman Sachs · ONSITE INTERVIEWGoldman Sachs interview questions
  • Maximum Data Transfer TimeMicrosoft · OAMicrosoft interview questions

Interview reports that mention this question

  • Google New Grad Referral Loop With Extra R1 Round: 1 Experience

Google interview experiences

  • Google interviews · November 2026
  • Google Software Engineer Onsite Questions: 10 Experiences
  • Google OA: What It Tests (2026)
  • Google L4 SWE Phone Screen: 1 Experience
  • Google ML Engineering Manager Onsite: 1 Experience
All Google interview questionsSee Google hiring insights
Saved
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public int farthestDistance(TreeNode root, int target) {
        // write your code here
    }
}
Checking account…