⌘K
Flycodx

CRED Reward Tree Good Nodes

CRED Reward Tree Good Nodes

MediumTrees0% accepted

CRED assigns reward points via a tree structure. A node X in the tree is a good node if in the path from root to X there are no nodes with a value greater than X's value. Given the root of a binary tree, return the count of good nodes.

Approach

Count nodes where no ancestor has a greater value (root-to-node path maximum equals node value).

Input

7 3 1 4 3 -1 1 5

Output

4
solution.py
Loading editor...
Test 1
Test 2
Test 3

Press Run to test your custom input, or Submit to judge against all test cases.

Sign in to continue

Create a free account to run code, submit solutions and track progress.

  • Free forever — no credit card
  • 10+ languages with live execution
  • Points, streaks & leaderboards
Auto Save OnLn 1, Col 1Spaces: 4UTF-8LFPython 3