Binary Tree Maximum Value - aloalgo

Binary Tree Maximum Value

Medium

You are given the root of a binary tree, find the maximum value among all nodes in the tree. The tree can contain positive, negative, or zero values.

A Binary Tree is a tree data structure in which each node has at most two children, referred to as the left child and the right child. The tree structure is represented using TreeNode objects, where each TreeNode has a val (the node's value), a left child, and a right child. You can assume that the root node is not null.

Example 1

Input
3920157
Output
20
Explanation:

The maximum value in the tree [3, 9, 20, null, null, 15, 7] is 20, which is found at the right child of the root's right child.

Example 2

Input
-10-5-15-12-8
Output
-5
Explanation:

Even with negative values, the largest value is -5, which is the root's left child.

Example 3

Input
7
Output
7
Explanation:

A single-node tree's maximum value is its own value.

Loading...
Input
3920157
Output
20

Hello! I am your ✨ AI assistant. I can provide you hints, explanations, give feedback on your code, and more. Just ask me anything related to the problem you're working on!