Grokking Data Structures & Algorithms for Coding Interviews
Vote

0% completed

Stretch Problem: Count Good Nodes in a Binary Tree (medium)

This problem is optional. The problems above cover what the chapter teaches. This one is here for readers who want a harder tree question, and nothing later in the course depends on it.

Problem Statement

You are given the root of a binary tree. A node is good when no node on the path from the root down to it holds a larger value.

The root is always good, because its path is only itself.

Return the number of good nodes in the tree.

Examples

Example 1

  • Input: root = [3, 1, 4, 3, null, 1, 5]
  • Expected Output: 4
  • Justification: The root 3 is good

.....

.....

.....

Like the course? Get enrolled and start learning!

Reading Progress

0%


Vote for new content