Practice
JavaScriptData StructuresReactConcepts
Sign in
← Back to problems

Maximum Value

Treesmedium

Implement `treeMax(root)` returning the largest value in the tree, or `null` for an empty tree.

Sample tests

Input: treeMax({"val":1,"left":{"val":2,"left":null,"right":null},"right":{"val":3,"left":null,"right":null}})
Output: 3
Input: treeMax({"val":9,"left":null,"right":null})
Output: 9

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • Using -Infinity as a base value is not JSON-friendly; return null for empty.

Learning resources

  • Wikipedia: Binary tree
Approach & explanation (try first)

The max is the node value compared against the non-null subtree maxima. O(n).

Loading...
⌘/Ctrl + Enter

Run your code to see results.