BackhardDynamic Programming

Maximum Path Product in a Tree Solution

Problem Statement

Given a tree where each node has a value, find the maximum product of node values along any path from the root to a leaf node, with the path consisting of distinct nodes.

Example 1
Input
{"tree":{"value":2,"left":{"value":3,"left":{"value":7,"left":null,"right":null},"right":{"value":5,"left":null,"right":null}},"right":{"value":4,"left":{"value":6,"left":null,"right":null},"right":{"value":8,"left":null,"right":null}}}}
Output
64

Explanation: The path with the maximum product from the root to a leaf is 2 -> 4 -> 8.

Constraints

  • The tree is guaranteed to be non-empty.
  • Each node has at most two children (left child and right child).
  • The number of nodes in the tree will not exceed 1000.
  • Each node's value will be between -10^5 and 10^5.
Live Compiler
Loading...
Test Cases & Output
🔒 Sign up to run your code

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free