# 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 invertTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
if not root:
return None
left = self.invertTree(root.left)
right = self.invertTree(root.right)
root.right = left
root.left = right
return root
I think its bottom up recursion. As we change the bottom values and proceed to root.
As @matszwecja stated there is no formal CS terminology for this.
However, if you are referring to these definitions of bottom-up and top-down recursion then it is bottom-up:
Bottom-up recursion: Processes from the leaves up to the root, performing main computations after reaching the base cases.
Top-down recursion: Starts processing at the root and may pass information down to child nodes before completing recursive calls.
Why?
invertTree
on root.left
and root.right
.