FastPrepCount Nodes That Top Their Root Path
Problems/Count Nodes That Top Their Root Path

Count Nodes That Top Their Root Path

Adobe logoAdobe● MediumFULLTIMEPHONE SCREEN

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: This problem counts the tree nodes that are at least as large as every node above them. The report is a post from March 2022 about a Computer Scientist interview in India (Java developer, personalization team), with the result still pending. Besides a talk on the author's project architecture, the first round linked two practice problems, the island-counting grid task and this one. The post gives no result for that round.

The rules here are those of the linked task: a value equal to the path maximum still counts, and the root is always good. Our choices: 1 to 1000 nodes with values from -10^4 to 10^4, and new examples.

Our version is about 95% the same as the reported question.

Given the root of a binary tree, return how many of its nodes are good.

A node is good when no node on the path from the root down to it has a larger value. A value equal to the largest one above it still counts, and the root is always good. Only the nodes on the node's own path matter, not nodes in other branches.

Function

countGoodNodes(root: TreeNode) → int

Examples

Example 1

root = [5,3,8,6,null,2,9]return = 4
root
536829

5 (the root) is good. 3 is not, because 5 is above it. 6 hangs under 3, and nothing on the path 5, 3, 6 is larger than 6, so 6 is good. 8 is good. Under 8, 2 is not good and 9 is. Good nodes: 5, 6, 8 and 9, so the answer is 4.

Example 2

root = [4,4,null,4,1]return = 3
root
4441

Every 4 is good, because a value equal to the largest one above it still counts. The 1 is not. The answer is 3.

Example 3

root = [-7]return = 1
root
-7

A tree with one node: the root is always good. The answer is 1.

Constraints

  • The tree has 1 to 1000 nodes.
  • -10^4 <= Node.val <= 10^4

More Adobe Phone Screen questions

  • Evaluate a Parenthesized Integer ExpressionPHONE SCREEN · Seen Oct 2026
  • All Elements of Two BSTs in OrderPHONE SCREEN · Seen Aug 2026
  • Rotate an Array In PlaceOA · PHONE SCREEN · Seen Aug 2026
  • Binary Tree Level-Order Traversal by LevelsPHONE SCREEN · ONSITE INTERVIEW · Seen Aug 2026
  • Count Occurrences in a Sorted ArrayPHONE SCREEN · ONSITE INTERVIEW · Seen Jul 2026
  • Count Pairs with Absolute Difference KPHONE SCREEN · Seen Jul 2026
  • Count Subsets with Sum KPHONE SCREEN · Seen Jul 2026
  • Numbers with Exactly Three DivisorsPHONE SCREEN · Seen Jul 2026
  • Sum of Even Nodes on a Path in a Numbered Binary TreePHONE SCREEN · Seen Jul 2026
  • Largest Value in Every Sliding WindowPHONE SCREEN · Seen Jul 2026

Similar questions at other companies

  • N-ary Index-Path Encoder and DecoderBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
  • House Robber IIIOkta · PHONE SCREENOkta interview questions
  • Render a Task Tree from CSV RowsStripe · PHONE SCREEN · ONSITE INTERVIEWStripe interview questions
  • Validate Binary Search TreeKotak Mahindra Bank · PHONE SCREENKotak Mahindra Bank interview questions
  • House Robber IIIGoldman Sachs · PHONE SCREENGoldman Sachs interview questions
  • Binary Tree Target-Sum PathsByteDance · PHONE SCREENByteDance interview questions
  • House Robber IIIVisa · PHONE SCREENVisa interview questions
  • Find All Paths for a Name in a File SystemThe Walt Disney Company · PHONE SCREENThe Walt Disney Company interview questions
  • Validate a BST with a Custom ComparisonApple · PHONE SCREENApple interview questions
  • Filter Undesired Comments and Their DescendantsReddit · PHONE SCREEN · ONSITE INTERVIEWReddit interview questions

Interview reports that mention this question

  • Adobe CS1 Interviews: 14 Experiences (Part 4 of 6)

Adobe interview experiences

  • Adobe Web, QA, DevOps, ML Roles: 13 Experiences (Part 3 of 3)
  • Adobe SWE Interviews, Level Unknown: 25 Experiences (Part 2 of 3)
  • Adobe Interviews, Level Unknown: 38 Experiences (Part 1 of 3)
  • Adobe New Grad MTS Interview Questions: 7 Experiences
  • Adobe ML/Research Intern Interviews: 12 Experiences (Part 4 of 4)
All Adobe interview questionsSee Adobe 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 countGoodNodes(TreeNode root) {
        // Write your code here.
    }
}
Checking account…