We are given the root node of a maximum tree: a tree where every node has a value greater than any other value in its subtree.
Just as in the previous problem, the given tree was constructed from an list A (root = Construct(A)) recursively with the following Construct(A) routine:

If A is empty, return null.
Otherwise, let A[i] be the largest element of A.  Create a root node with value A[i].
The left child of root will be Construct([A[0], A[1], ..., A[i-1]])
The right child of root will be Construct([A[i+1], A[i+2], ..., A[A.length - 1]])
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).
 
Example 1:

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:

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:

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:

1 <= B.length <= 100

Difficulty: interview

Starter code:

# Definition for a binary tree node.
class TreeNode:
    def __init__(self, val=0, left=None, right=None):
    self.val = val
    self.left = left
    self.right = right
class Solution:
    def insertIntoMaxTree(self, root: TreeNode, val: int) -> TreeNode:
        

Solutions:
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def insertIntoMaxTree(self, root: TreeNode, val: int) -> TreeNode:
        if root and root.val > val:
            root.right = self.insertIntoMaxTree(root.right, val)
            return root
        node = TreeNode(val)
        node.left = root
        return node

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def insertIntoMaxTree(self, root: TreeNode, val: int) -> TreeNode:
        if root is None:
            return TreeNode(val)
        elif val > root.val:
            n = TreeNode(val)
            n.left = root
            return n
        else:
            n = root
            p = None
            while n is not None and val < n.val:
                n, p = n.right, n
            m = TreeNode(val)
            p.right = m
            m.left = n
            return root

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
  def insertIntoMaxTree(self, root: TreeNode, val: int) -> TreeNode:
    if root and root.val > val:
      root.right = self.insertIntoMaxTree(root.right, val)
      return root

    node = TreeNode(val)
    node.left = root
    return node

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right

'''
Also recursive.
Base case: old tree is empty
    - TreeNode(new elem, None, None)
Case 1: new elem > max elem in tree.
    - Then new tree should be TreeNode(new elem, old tree, None)
Case 2: new elem < max elem in tree
    - Then right subtree needs to be updated (recurse)
    - New tree = TreeNode(max elem, left subtree, new right subtree)
'''


class Solution:
    def insertIntoMaxTree(self, root: TreeNode, val: int) -> TreeNode:
        if not root:
            return TreeNode(val, None, None)
        if val > root.val:
            return TreeNode(val, root, None)
        else:
            # val < root.val
            return TreeNode(root.val, root.left, self.insertIntoMaxTree(root.right, val))

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def insertIntoMaxTree(self, root: TreeNode, val: int) -> TreeNode:
        node = TreeNode(val)
        if not root:
            return node
        if val > root.val:
            node.left = root
            return node
        right = self.insertIntoMaxTree(root.right, val)
        root.right = right
        return root

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def findTree(self,root,node):
        if not root:
            return node
        if node.val>root.val:
            node.left = root
            return node
        root.right = self.findTree(root.right, node)
        return root
        
    def insertIntoMaxTree(self, root: TreeNode, val: int) -> TreeNode:
        return self.findTree(root, TreeNode(val))

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    
    def insertIntoMaxTree(self, root: TreeNode, val: int) -> TreeNode:
        def helper(root,val):
            if root is None:
                return TreeNode(val)
            if val > root.val:
                return TreeNode(val,root,None)
            root.right = helper(root.right,val)
            return root
        return helper(root,val)        


# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def insertIntoMaxTree(self, root: TreeNode, val: int) -> TreeNode:
        node = TreeNode(val)
        if not root or val > root.val:
            node.left = root
            return node
        else:
            root.right = self.insertIntoMaxTree(root.right, val)
            return root

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def insertIntoMaxTree(self, root: TreeNode, val: int) -> TreeNode:
            
        if not root: return TreeNode(val)
        
        if root.val<val: 
            node = TreeNode(val)
            node.left=root
            return node
        else:
            root.right = self.insertIntoMaxTree(root.right, val)
                    
        return root
    
    
        # if(root==None):
        #     return TreeNode(val)       
        # if(root.val<val):
        #         temp = TreeNode(val)
        #         temp.left = root
        #         return temp
        # else:
        #         root.right = self.insertIntoMaxTree(root.right, val)
        # return root    


# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    
    def insertIntoMaxTree(self, root: TreeNode, val: int) -> TreeNode:
        self.arr = []
        def inorder(root):
            if root:
                inorder(root.left)
                self.arr.append(root.val)
                inorder(root.right)
        def solve(nums):
            if len(nums):
                #print(nums)
                mx = nums.index(max(nums))
                root  = TreeNode(nums[mx])
                root.left = solve(nums[:mx])
                root.right = solve(nums[mx+1:])
                return root
        
    
        inorder(root)
        self.arr.append(val)
        return solve(self.arr)