Problem · Tree
Maximum Value in a Binary Tree
Learn this problemProblem statement
Given the root of a nonempty binary tree, return the maximum value stored in any node.
Your implementation must handle trees whose values are all negative.
Function
findMaximumValue(root: TreeNode) → intExamples
Example 1
root = [3,1,5,null,2,4,8]return = 8The largest node value is 8.
Example 2
root = [-8,-12,-3]return = -3All values are negative, so the least negative value is the maximum.
Constraints
- The tree contains from
1through10^5nodes. -10^9 <= Node.val <= 10^9.