music-kraken-core/src/try_python.py
Hellow 5a699c3937 STARTED IMPLEMENTING DB
STARTED IMPLEMENTING DB
2023-02-14 23:07:16 +01:00

42 lines
1017 B
Python

from typing import Optional
class TreeNode:
"""A class representing a binary tree node."""
def __init__(self, val: int = 0, left: Optional['TreeNode'] = None, right: Optional['TreeNode'] = None):
"""
Initializes a new instance of the TreeNode class.
Args:
val: The value of the node.
left: The left child of the node.
right: The right child of the node.
"""
self.val = val
self.left = left
self.right = right
def invert_tree(root: Optional[TreeNode]) -> Optional[TreeNode]:
"""
Inverts a binary tree.
Args:
root: The root node of the binary tree.
Returns:
The root node of the inverted binary tree.
"""
if root is None:
return None
# Swap left and right children of the root node
root.left, root.right = root.right, root.left
# Recursively invert the left and right subtrees
invert_tree(root.left)
invert_tree(root.right)
return root