156. Binary Tree Upside Down (Medium) - TengnanYao/daily_leetcode GitHub Wiki
# 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 upsideDownBinaryTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
if not root:
return None
stack = []
while root:
stack.append(root)
root = root.left
root = node = stack.pop()
while stack:
right = stack.pop()
node.right = right
node.left = right.right
right.right = None
right.left = None
node = node.right
return root