Grokking 75: Top Coding Interview Questions
Ask Author
Back to course home

0% completed

Solution: Count Good Nodes in Binary Tree
Table of Contents

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Problem Statement

Given a binary tree root, return the number of good nodes in the binary tree.

A node is considered good if in the path from the root to this node, there are no nodes with a value greater than this node's value.

Examples

Example 1

  • Input: root = [3, 1, 3, 3, null, 1, 5]
  • Expected Output: 4
  • Explanation:
    • Root node (3) is always good.
    • Node 5 is also a good node.
    • Both nodes 3 are good.

Example 2

  • Input: root = [2, 3, 4, 1, null, null, 5]
  • Expected Output: 4
  • Explanation:
    • Nodes 2, 3, `4

.....

.....

.....

Like the course? Get enrolled and start learning!

Table of Contents

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible