> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Maximum Binary Tree II Python Solution

> Tested Python solution for LeetCode 998 with 16 pytest cases. Generate a practice environment with lcpy.

LeetCode 998, [Medium](/catalog/medium). Topics: [Tree](/catalog/topics/tree), [Binary Tree](/catalog/topics/binary-tree). [View on LeetCode](https://leetcode.com/problems/maximum-binary-tree-ii/description/).

Generate this problem as a practice environment: tested reference solution, 16 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 998   # by problem number
lcpy gen -s maximum_binary_tree_ii   # by problem name
```

## Problem

A maximum tree is a tree where every node has a value greater than any other value in its subtree.

You are given the `root` of a maximum binary tree and an integer `val`.

Just as in the [previous problem](https://leetcode.com/problems/maximum-binary-tree/), the given tree was constructed from a list `a` (`root = Construct(a)`) recursively with the following `Construct(a)` routine:

1. If `a` is empty, return `null`.
2. Otherwise, let `a[i]` be the largest element of `a`. Create a root node with the value `a[i]`.
3. The left child of `root` will be `Construct([a[0], a[1], ..., a[i - 1]])`.
4. The right child of `root` will be `Construct([a[i + 1], a[i + 2], ..., a[a.length - 1]])`.
5. Return `root`.

Note that we were not given `a` directly, only a root node `root = Construct(a)`.

Suppose `b` is a copy of `a` with the value `val` appended to it. It is guaranteed that `b` has unique values.

Return `Construct(b)`.

### Examples

![Example 1](https://assets.leetcode.com/uploads/2021/08/09/maxtree1.JPG)

```
Input: root = [4,1,3,null,null,2], val = 5
Output: [5,4,null,1,3,null,null,2]
Explanation: a = [1,4,2,3], b = [1,4,2,3,5]
```

![Example 2](https://assets.leetcode.com/uploads/2021/08/09/maxtree21.JPG)

```
Input: root = [5,2,4,null,1], val = 3
Output: [5,2,4,null,1,null,3]
Explanation: a = [2,1,5,4], b = [2,1,5,4,3]
```

![Example 3](https://assets.leetcode.com/uploads/2021/08/09/maxtree3.JPG)

```
Input: root = [5,2,3,null,1], val = 4
Output: [5,2,4,null,1,3]
Explanation: a = [2,1,5,3], b = [2,1,5,3,4]
```

### Constraints

* The number of nodes in the tree is in the range \[1, 100].
* 1 \<= Node.val \<= 100
* All the values of the tree are unique.
* 1 \<= val \<= 100

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/maximum_binary_tree_ii/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/maximum_binary_tree_ii/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
from leetcode_py import TreeNode


class Solution:
    # Time: O(h) where h is the tree height, O(n) worst case
    # Space: O(h) recursion stack, O(n) worst case
    def insert_into_max_tree(self, root: TreeNode[int] | None, val: int) -> TreeNode[int] | None:
        if root is None or val > root.val:
            return TreeNode(val, root, None)
        root.right = self.insert_into_max_tree(root.right, val)
        return root
```

## Complexity

| Time | Space |
| - | - |
| O(h) where h is the tree height, O(n) worst case | O(h) recursion stack, O(n) worst case |

## Tags


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.